Spaces:
Sleeping

AGI / Dockerfile
Dmitry Beresnev
fix security volnurabilities
70dee29
Raw History Blame Contribute Delete
8.1 kB
# debian:bookworm-slim pinned by digest (resolved 2026-11-13). Bump deliberately.
FROM debian:bookworm-slim@sha256:734a72b3d6142a71993b4bc580ca01d8ca687f77a2c422d96a961d3376b179bc AS builder
# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
git \
build-essential \
cmake \
ninja-build \
ca-certificates \
libcurl4-openssl-dev \
libssl-dev \
libboost-dev \
libopenblas-dev \
nlohmann-json3-dev \
pkg-config \
&& rm -rf /var/lib/apt/lists/*
# Clone and build llama.cpp with SSL support for HuggingFace Hub
WORKDIR /build
ARG CACHEBUST=6
# Pinned to an immutable commit (release tag b12367). Tags can be moved;
# commits cannot. Bump deliberately and re-run the endpoint tests.
ARG LLAMA_CPP_REF=fe14cad51bbfb3f1cc8d9cfd9d70e98a123743d3
#ARG BUILD_PROFILE=fast_build
ARG BUILD_PROFILE=fast_runtime
ARG BUILD_JOBS=1
RUN mkdir llama.cpp && cd llama.cpp && git init -q && \
git remote add origin https://github.com/ggerganov/llama.cpp.git && \
git fetch --depth 1 origin ${LLAMA_CPP_REF} && \
git checkout -q FETCH_HEAD && \
echo "llama.cpp commit: $(git rev-parse HEAD)" && \
if [ "${BUILD_PROFILE}" = "fast_runtime" ]; then \
C_FLAGS="-O3 -DNDEBUG"; \
CXX_FLAGS="-O3 -DNDEBUG"; \
BLAS_FLAG="-DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS"; \
JOBS="${BUILD_JOBS}"; \
else \
C_FLAGS="-O1 -DNDEBUG"; \
CXX_FLAGS="-O1 -DNDEBUG"; \
BLAS_FLAG="-DGGML_BLAS=OFF"; \
JOBS="1"; \
fi && \
cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_C_FLAGS_RELEASE="${C_FLAGS}" \
-DCMAKE_CXX_FLAGS_RELEASE="${CXX_FLAGS}" \
-DLLAMA_BUILD_TESTS=OFF \
-DLLAMA_BUILD_EXAMPLES=OFF \
-DLLAMA_BUILD_SERVER=ON \
-DGGML_NATIVE=OFF \
-DGGML_AVX2=ON \
-DGGML_AVX=ON \
-DGGML_FMA=ON \
-DGGML_F16C=ON \
-DGGML_OPENMP=ON \
${BLAS_FLAG} \
-DLLAMA_CURL=ON \
-DLLAMA_OPENSSL=ON && \
cmake --build build --config Release --target llama-server -j"${JOBS}" && \
echo "=== Binary dependencies ===" && \
ldd build/bin/llama-server || true && \
mkdir -p /build/llama-libs && \
find build -type f \( -name '*.so' -o -name '*.so.*' \) -exec cp -v {} /build/llama-libs/ \; || true
# Build C++ manager (Boost.Beast + JSON)
COPY cpp/ /build/cpp/
RUN g++ -std=c++17 -O2 -DBOOST_ERROR_CODE_HEADER_ONLY -pthread \
-I/build/cpp \
/build/cpp/config.cpp \
/build/cpp/http_helpers.cpp \
/build/cpp/llm_manager.cpp \
/build/cpp/llm_manager_types.cpp \
/build/cpp/model_manager.cpp \
/build/cpp/request_parsing.cpp \
/build/cpp/runtime_components.cpp \
/build/cpp/server.cpp \
-o /build/llm-manager
# Runtime stage
FROM debian:bookworm-slim@sha256:734a72b3d6142a71993b4bc580ca01d8ca687f77a2c422d96a961d3376b179bc
# Install runtime dependencies including SSL/HTTPS support
RUN apt-get update && apt-get install -y --no-install-recommends \
libcurl4 \
ca-certificates \
libgomp1 \
libopenblas0-pthread \
libstdc++6 \
openssl \
&& rm -rf /var/lib/apt/lists/*
# Copy llama-server binary and all shared libraries from builder
COPY --from=builder /build/llama.cpp/build/bin/llama-server /usr/local/bin/llama-server
COPY --from=builder /build/llama-libs/ /usr/local/lib/
COPY --from=builder /build/llm-manager /usr/local/bin/llm-manager
# Update library cache
RUN ldconfig
# The legacy Python app (python/) is intentionally NOT shipped: it has
# unauthenticated admin endpoints and a pile of unpinned dependencies. The
# runtime image contains only llama-server and the C++ manager.
# Create non-root user
RUN useradd -m -u 1000 user && \
mkdir -p /home/user/.cache/llama.cpp && \
chown -R user:user /home/user
USER user
WORKDIR /home/user
# Set environment variables
ENV HOME=/home/user \
LLAMA_CACHE=/home/user/.cache/llama.cpp \
PATH=/home/user/.local/bin:$PATH
EXPOSE 7860
# The only supported entrypoint is the C++ manager (CMD at the bottom). The
# legacy FastAPI app is unauthenticated and is not in this image.
#
# Historical examples of running llama-server directly (unauthenticated —
# do not use in production):
# Start FastAPI app (which manages llama-server internally)
#CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
# --- Optional: run llama.cpp C++ server directly (temporary rollout) ---
# Keep the FastAPI CMD above as the default. Uncomment ONE of the following
# to run the C++ server directly instead of the Python app.
#
# Example DeepSeek (4k context):
# CMD ["llama-server", "-hf", "TheBloke/deepseek-llm-7B-chat-GGUF:deepseek-llm-7b-chat.Q4_K_M.gguf",
# "--host", "0.0.0.0", "--port", "7860", "-c", "4096", "-t", "4", "-ngl", "0", "--cont-batching", "-b", "512"]
#
# Example Mixtral-8x7B-Instruct (known loader incompatibilities on newer llama.cpp + too large for 10GB RAM):
# CMD ["llama-server", "-hf", "TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF:mixtral-8x7b-instruct-v0.1.Q4_K_M.gguf", "--host", "0.0.0.0", "--port", "7860", "-c", "4096", "-t", "4", "-ngl", "0", "--cont-batching", "-b", "64", "--ubatch-size", "32"]
# CMD ["llama-server", "-hf", "QuantFactory/Qwen2.5-7B-Instruct-GGUF:q4_k_m", "--host", "0.0.0.0", "--port", "7860", "-c", "8192", "-t", "4", "-ngl", "0", "--cont-batching", "-b", "128", "--ubatch-size", "64"]
#
# Active manager process:
# - loads default model at startup
# - supports /switch-model runtime model change
# - proxies /v1/chat/completions to active worker
# Tuned for the free HF Space (~2 vCPU, CPU-only, OpenBLAS build):
# - MODEL_THREADS matches the vCPU quota; more threads thrash a compute-bound
# generation loop (was 4).
# - MODEL_BATCH/UBATCH sized so the OpenBLAS prompt-eval path actually gets
# batches worth multiplying (was 64/32). RAM headroom (~16 GB vs ~5 GB used)
# covers the larger compute buffers.
# Validate any change with tests/bench.py (see PERFORMANCE_REFACTORING_PLAN.md).
#
# Security (see docs/SECURITY_AUDIT_AND_FIX_PLAN.md):
# - API keys are NOT baked into the image. Set the API_KEYS secret on the
# Space: API_KEYS="admin-main:admin:<32+ char secret>,user-main:user:<secret>"
# (a secret may also be given as sha256:<hex>). Without it the manager
# fails closed and every authenticated endpoint returns 401.
# - ALLOWED_MODELS restricts /switch-model; the default model is always allowed.
# - PER_IP_REQUESTS_PER_MINUTE is a pre-auth limit per client address
# (X-Forwarded-For from the HF proxy).
# - MIN_FREE_DISK_MB refuses a model switch when the cache volume is nearly full.
# - The allowlist below is the curated set from docs/MODEL_CANDIDATES_FREE_SPACE.md:
# dense upgrades (Qwen3.5), MoE fast lanes (LFM2.5-8B-A1B, Granite 4.0 H-Tiny,
# 1-1.5B active), Gemma 4, and one large MoE (Qwen3.6-35B-A3B at IQ3, 12.3 GiB)
# that needs SWITCH_TIMEOUT_SEC >= 600 on a cold start.
ENV ALLOWED_MODELS=QuantFactory/Qwen2.5-7B-Instruct-GGUF:q4_k_m,unsloth/Qwen3.5-9B-GGUF:Q4_K_M,unsloth/Qwen3.5-4B-GGUF:Q4_K_M,LiquidAI/LFM2.5-8B-A1B-GGUF:Q4_K_M,unsloth/granite-4.0-h-tiny-GGUF:Q4_K_M,unsloth/gemma-4-E4B-it-GGUF:Q4_K_M,unsloth/gemma-4-E2B-it-GGUF:Q4_K_M,unsloth/Qwen3.6-35B-A3B-GGUF:UD-IQ3_XXS \
PER_IP_REQUESTS_PER_MINUTE=120 \
MANAGER_MAX_SESSIONS=256 \
MAX_REQUEST_BODY_BYTES=1048576 \
LOG_REQUEST_BODIES=false
ENV DEFAULT_MODEL=QuantFactory/Qwen2.5-7B-Instruct-GGUF:q4_k_m \
MANAGER_HOST=0.0.0.0 \
MANAGER_PORT=7860 \
WORKER_BASE_PORT=8080 \
SWITCH_TIMEOUT_SEC=600 \
REQUEST_TIMEOUT_SEC=300 \
DEFAULT_MAX_TOKENS=2048 \
MAX_TOKENS_PER_REQUEST=4096 \
MODEL_N_CTX=8192 \
MODEL_THREADS=2 \
MODEL_NGL=0 \
MODEL_BATCH=256 \
MODEL_UBATCH=128
CMD ["llm-manager"]
#
# Example Qwen2.5-Coder 7B Instruct (32k context):
# CMD ["llama-server", "-hf", "Qwen/Qwen2.5-Coder-7B-Instruct-GGUF:Qwen2.5-Coder-7B-Instruct.Q4_K_M.gguf",
# "--host", "0.0.0.0", "--port", "7860", "-c", "32768", "-t", "4", "-ngl", "0", "--cont-batching", "-b", "512"]