File size: 13,246 Bytes
66958f4
d134bdd
0915bee
dc89243
0915bee
dc89243
0915bee
dc89243
d134bdd
 
18880bd
e2dac86
18880bd
 
 
 
 
 
 
 
 
 
 
 
 
 
e2dac86
d134bdd
dc89243
18880bd
 
517287f
18880bd
 
 
 
 
e2dac86
18880bd
517287f
dc89243
 
18880bd
0915bee
 
18880bd
 
 
0915bee
 
 
 
 
 
 
 
 
18880bd
 
 
a735611
18880bd
 
 
 
 
 
 
 
 
 
 
 
 
0915bee
 
 
dc89243
 
c369480
0915bee
 
dc89243
 
 
0915bee
 
 
 
dc89243
0915bee
 
dc89243
 
 
c369480
 
 
dc89243
0915bee
 
18880bd
 
 
 
 
 
 
 
 
 
 
 
0915bee
18880bd
 
 
0915bee
18880bd
dc89243
 
 
 
 
 
 
 
c369480
 
dc89243
 
 
 
d134bdd
dc89243
a3f2d83
dc89243
c369480
dc89243
d134bdd
0915bee
 
 
 
c369480
 
 
 
 
d134bdd
 
 
 
0915bee
 
c369480
0915bee
c369480
 
 
dc89243
18880bd
 
dc89243
d134bdd
dc89243
 
d134bdd
0915bee
 
 
 
dc89243
d134bdd
dc89243
 
 
 
 
 
 
d134bdd
0915bee
 
18880bd
0915bee
dc89243
d134bdd
c369480
 
 
dc89243
c369480
dc89243
c369480
d134bdd
 
dc89243
18880bd
dc89243
 
d134bdd
dc89243
c369480
 
e09f03f
0915bee
 
 
 
18880bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0915bee
dc89243
 
 
 
 
18880bd
dc89243
 
 
 
 
0915bee
18880bd
 
 
0915bee
dc89243
 
c369480
dc89243
d134bdd
0915bee
 
18880bd
0915bee
 
 
 
 
 
 
 
18880bd
 
 
 
 
 
 
 
 
 
 
0915bee
 
 
dc89243
 
 
 
 
 
d134bdd
dc89243
 
d134bdd
0915bee
 
 
 
c369480
 
 
dc89243
 
 
 
 
d134bdd
dc89243
 
18880bd
 
 
 
 
 
 
 
3a1ed88
0915bee
18880bd
c369480
0915bee
c369480
0915bee
dc89243
 
d134bdd
c369480
18880bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c369480
0915bee
c369480
dc89243
c369480
dc89243
 
c369480
 
0915bee
c369480
dc89243
 
 
 
 
 
 
d134bdd
 
dc89243
0915bee
 
18880bd
 
 
 
dc89243
0915bee
dc89243
2dbe9ce
0915bee
2dbe9ce
0915bee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18880bd
0915bee
 
 
 
 
 
 
 
18880bd
 
 
0915bee
 
 
 
 
 
 
 
 
18880bd
0915bee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc89243
 
 
0915bee
dc89243
d134bdd
943fe4c
dc89243
 
0915bee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# syntax=docker/dockerfile:1.4


# ============================================================
# Stage 1: Build CPU-optimized OpenBLAS + llama.cpp
# ============================================================
FROM debian:13 AS native-build

ENV DEBIAN_FRONTEND=noninteractive


# ============================================================
# Native build dependencies
#
# Original packages retained:
#   build-essential
#   curl
#   ca-certificates
#   git
#   cmake
#   clang
#   pkg-config
#   ccache
#   wget
#
# gfortran is required for the source OpenBLAS build.
# ============================================================
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        build-essential \
        curl \
        ca-certificates \
        git \
        cmake \
        clang \
        pkg-config \
        ccache \
        wget \
        gfortran && \
    update-ca-certificates && \
    rm -rf /var/lib/apt/lists/*


# ============================================================
# Build OpenBLAS from source
#
# No TARGET is specified.
# OpenBLAS detects the build CPU and optimizes for it.
# ============================================================
WORKDIR /build

RUN git clone --depth 1 \
    https://github.com/OpenMathLib/OpenBLAS.git \
    /build/OpenBLAS

WORKDIR /build/OpenBLAS


# Suppress the enormous normal compiler log.
# If compilation fails, print the final 100 lines.
RUN make -j2 > /tmp/openblas-build.log 2>&1 || \
    (echo "OpenBLAS build failed:" && \
     tail -100 /tmp/openblas-build.log && \
     false) && \
    make PREFIX=/opt/OpenBLAS install \
        > /tmp/openblas-install.log 2>&1 || \
    (echo "OpenBLAS install failed:" && \
     tail -100 /tmp/openblas-install.log && \
     false) && \
    rm -f \
        /tmp/openblas-build.log \
        /tmp/openblas-install.log


# ============================================================
# Build llama.cpp against our source-built OpenBLAS
# ============================================================
WORKDIR /build

RUN git clone --depth 1 \
    https://github.com/ggml-org/llama.cpp.git \
    /build/llama.cpp

WORKDIR /build/llama.cpp

ENV PKG_CONFIG_PATH=/opt/OpenBLAS/lib/pkgconfig
ENV LD_LIBRARY_PATH=/opt/OpenBLAS/lib

RUN cmake -S . -B build \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_PREFIX_PATH=/opt/OpenBLAS \
        -DGGML_NATIVE=ON \
        -DGGML_BLAS=ON \
        -DGGML_BLAS_VENDOR=OpenBLAS \
        -DLLAMA_CURL=OFF && \
    cmake --build build \
        --config Release \
        -j2


# ============================================================
# Extract runtime files
#
# IMPORTANT:
# Use libopenblas* as in your ORIGINAL Dockerfile.
#
# This copies:
#   libopenblas.so
#   libopenblas.so.0
#   CPU-specific target library such as:
#   libopenblas_skylakexp-r0.3.34.dev.so
#
# This avoids creating broken OpenBLAS symlinks.
# ============================================================
RUN mkdir -p \
        /out/llama \
        /out/openblas && \
    cp -a build/bin/. /out/llama/ && \
    cp -a /opt/OpenBLAS/lib/libopenblas* /out/openblas/


# ============================================================
# Stage 2: Build .NET application
# ============================================================
FROM debian:13 AS dotnet-build

ENV DEBIAN_FRONTEND=noninteractive
ENV DOTNET_CLI_TELEMETRY_OPTOUT=1
ENV DOTNET_NOLOGO=1

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        ca-certificates \
        wget \
        git \
        libicu76 \
        libssl3t64 && \
    update-ca-certificates && \
    rm -rf /var/lib/apt/lists/*


# ============================================================
# Create non-root build user
# ============================================================
RUN useradd -m user && \
    mkdir -p \
        /home/user/code \
        /home/user/out/app && \
    chown -R user:user /home/user

USER user
WORKDIR /home/user


# ============================================================
# Install .NET 10 SDK
# ============================================================
RUN wget -q \
        https://dot.net/v1/dotnet-install.sh \
        -O dotnet-install.sh && \
    chmod +x dotnet-install.sh && \
    ./dotnet-install.sh \
        --channel 10.0 && \
    rm dotnet-install.sh

ENV DOTNET_ROOT=/home/user/.dotnet
ENV PATH="${PATH}:${DOTNET_ROOT}:${DOTNET_ROOT}/tools"


# ============================================================
# Clone application repositories
# ============================================================
WORKDIR /home/user/code

RUN --mount=type=secret,id=GITHUB_TOKEN,mode=0444,required=true \
    git clone --depth 1 \
        https://x-access-token:$(cat /run/secrets/GITHUB_TOKEN)@github.com/Mungert69/NetworkMonitorLib.git \
        /home/user/code/NetworkMonitorLib && \
    git clone --depth 1 \
        https://x-access-token:$(cat /run/secrets/GITHUB_TOKEN)@github.com/Mungert69/NetworkMonitorLLM.git \
        /home/user/code/NetworkMonitorLLM


# ============================================================
# Restore and publish .NET application
# ============================================================
WORKDIR /home/user/code/NetworkMonitorLLM

RUN dotnet restore NetworkMonitorLLM.csproj

RUN dotnet publish NetworkMonitorLLM.csproj \
        -c Release \
        --no-restore \
        --no-self-contained \
        -o /home/user/out/app


# ============================================================
# Stage 3: Runtime image
# ============================================================
FROM debian:13 AS runtime

ENV DEBIAN_FRONTEND=noninteractive
ENV DOTNET_CLI_TELEMETRY_OPTOUT=1
ENV DOTNET_NOLOGO=1


# ============================================================
# Runtime dependencies
#
# Original runtime utilities retained:
#   curl
#   ca-certificates
#   wget
#   vim
#   libicu76
#   expect
#
# Explicit compiler/runtime libraries added because the build
# toolchain no longer exists in this multi-stage image:
#
#   libstdc++6
#   libgcc-s1
#   libgfortran5
#   libgomp1
#   libquadmath0
#   libatomic1
# ============================================================
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        ca-certificates \
        wget \
        curl \
        vim \
        libicu76 \
        libssl3t64 \
        libgssapi-krb5-2 \
        libstdc++6 \
        libgcc-s1 \
        libgfortran5 \
        libgomp1 \
        libquadmath0 \
        libatomic1 \
        zlib1g \
        expect \
        tzdata && \
    update-ca-certificates && \
    rm -rf /var/lib/apt/lists/*


# ============================================================
# Install CPU-optimized OpenBLAS
# ============================================================
COPY --from=native-build \
    /out/openblas/ \
    /usr/local/lib/

RUN ldconfig


# ============================================================
# Verify OpenBLAS itself was copied correctly
#
# `test -e` follows the libopenblas.so symlink, so this fails
# if the CPU-specific target library was not copied.
# ============================================================
RUN test -e /usr/local/lib/libopenblas.so && \
    echo "OpenBLAS library verified:" && \
    ls -lh /usr/local/lib/libopenblas*


# ============================================================
# Create runtime user/directories
# ============================================================
RUN useradd -m user && \
    mkdir -p \
        /home/user/code/app/wwwroot \
        /home/user/code/models/llama.cpp \
        /home/user/code/models && \
    chown -R user:user /home/user

USER user
WORKDIR /home/user


# ============================================================
# Install ONLY ASP.NET Core runtime
# ============================================================
RUN wget -q \
        https://dot.net/v1/dotnet-install.sh \
        -O dotnet-install.sh && \
    chmod +x dotnet-install.sh && \
    ./dotnet-install.sh \
        --channel 10.0 \
        --runtime aspnetcore && \
    rm dotnet-install.sh

ENV DOTNET_ROOT=/home/user/.dotnet
ENV PATH="${PATH}:${DOTNET_ROOT}:${DOTNET_ROOT}/tools"


# ============================================================
# Runtime dynamic library locations
#
# llama.cpp puts its shared libraries beside its executables.
# Source-built OpenBLAS is in /usr/local/lib.
# ============================================================
ENV LD_LIBRARY_PATH="/home/user/code/models/llama.cpp:/usr/local/lib"


# ============================================================
# Copy compiled llama.cpp binaries/libraries
# ============================================================
COPY --from=native-build --chown=user:user \
    /out/llama/ \
    /home/user/code/models/llama.cpp/


# ============================================================
# Verify EVERY llama/OpenBLAS dynamic dependency
#
# This catches missing shared libraries DURING THE DOCKER BUILD
# rather than discovering them after the Space starts.
# ============================================================
RUN set -eu; \
    echo "Checking llama.cpp runtime dependencies..."; \
    missing=0; \
    for f in /home/user/code/models/llama.cpp/*; do \
        [ -f "$f" ] || continue; \
        deps="$(ldd "$f" 2>/dev/null || true)"; \
        if printf '%s\n' "$deps" | grep -q "not found"; then \
            echo "========================================"; \
            echo "MISSING DEPENDENCY IN: $f"; \
            printf '%s\n' "$deps"; \
            echo "========================================"; \
            missing=1; \
        fi; \
    done; \
    for f in /usr/local/lib/libopenblas*; do \
        [ -f "$f" ] || continue; \
        deps="$(ldd "$f" 2>/dev/null || true)"; \
        if printf '%s\n' "$deps" | grep -q "not found"; then \
            echo "========================================"; \
            echo "MISSING DEPENDENCY IN: $f"; \
            printf '%s\n' "$deps"; \
            echo "========================================"; \
            missing=1; \
        fi; \
    done; \
    if [ "$missing" -ne 0 ]; then \
        echo "One or more runtime libraries are missing."; \
        exit 1; \
    fi; \
    echo "All llama.cpp/OpenBLAS dynamic dependencies resolved."


# ============================================================
# Copy published .NET application
# ============================================================
COPY --from=dotnet-build --chown=user:user \
    /home/user/out/app/ \
    /home/user/code/app/


# ============================================================
# Application configuration
# ============================================================
COPY --chown=user:user \
    appsettings.json \
    /home/user/code/app/appsettings.json

COPY --chown=user:user \
    index.html \
    /home/user/code/app/wwwroot/index.html


# ============================================================
# Model configuration
#
# Model is deliberately NOT put into the Docker image.
# It is downloaded after the Space starts so the 6.5GB GGUF
# does not have to be exported/compressed/pushed as an image
# layer.
# ============================================================
ENV MODEL_DIR=/home/user/code/models

ENV MODEL_FILE=Mellum2-12B-A2.5B-Instruct-mxfp4_moe.gguf

ENV MODEL_URL=https://huggingface.co/Mungert/Mellum2-12B-A2.5B-Instruct-GGUF/resolve/main/Mellum2-12B-A2.5B-Instruct-mxfp4_moe.gguf


# ============================================================
# Create startup script
# ============================================================
RUN cat > /home/user/start.sh <<'EOF'
#!/bin/sh

set -e

MODEL_PATH="${MODEL_DIR}/${MODEL_FILE}"
TEMP_PATH="${MODEL_PATH}.download"

echo "=========================================="
echo "NetworkMonitorLLM startup"
echo "=========================================="

echo "Model:"
echo "${MODEL_PATH}"

echo
echo "OpenBLAS:"
ls -lh /usr/local/lib/libopenblas* || true

echo
echo "llama.cpp:"
ls -lh /home/user/code/models/llama.cpp/ | head -20 || true


# ============================================================
# Download model at runtime
#
# Quiet mode prevents the 6.5GB download from exhausting the
# Hugging Face web log display.
# ============================================================
if [ ! -s "${MODEL_PATH}" ]; then

    echo
    echo "Model not present."
    echo "Downloading model..."

    rm -f "${TEMP_PATH}"

    wget -q \
        --tries=5 \
        --timeout=60 \
        -O "${TEMP_PATH}" \
        "${MODEL_URL}"

    echo "Download completed."

    mv "${TEMP_PATH}" "${MODEL_PATH}"

else

    echo
    echo "Model already exists."

fi


echo
echo "Model size:"
ls -lh "${MODEL_PATH}"


# ============================================================
# Start .NET application
# ============================================================
echo
echo "Starting NetworkMonitorLLM..."

cd /home/user/code/app

exec dotnet NetworkMonitorLLM.dll \
    --urls http://0.0.0.0:7860
EOF

RUN chmod +x /home/user/start.sh


# ============================================================
# Hugging Face Space
# ============================================================
WORKDIR /home/user/code/app

EXPOSE 7860

CMD ["/home/user/start.sh"]