# syntax=docker/dockerfile:1
#
# UnbiasedStrata on a Pascal card (Tesla P40 / GTX 10-series, sm_61).
#
# The engines (strata, strata-poly, strata-dsv41) and the C++ front end are
# compiled during docker build.  The front end serves the OpenAI-style API and
# starts the engine named by --engine as a child process; a bare engine name is
# looked up BESIDE the front end binary, so all of them must sit in the same
# directory.  Nothing is downloaded at build or run time: the model shards, the
# pack and the draft (MTP) head must be mounted.  See README.md.
#
# Build:
#   docker build -t unbiased-strata:p40 .
#
# Run (host needs an NVIDIA driver and nvidia-container-toolkit):
#   docker run --rm --gpus '"device=2"' -p 8082:8080 \
#     -v /path/models:/models:ro -v /path/Strata-data:/strata:ro \
#     unbiased-strata:p40 --engine strata-poly -m /models/model.gguf \
#       --host 0.0.0.0 --port 8080 -- --ctx 32768
#
# PASCAL=1 compiles sm_61 and turns on the engine's STRATA_EXPERIMENTAL_SM60
# path.  It needs a CUDA 12.x toolkit (CUDA 13 dropped sm_60/sm_70); 12.8.1 is
# the last 12.x release and still supports Pascal.  Override CUDA_ARCHS to build
# for another card, e.g. --build-arg CUDA_ARCHS=86 for an RTX 30.

FROM nvidia/cuda:12.8.1-devel-ubuntu24.04 AS build

ARG CUDA_ARCHS=61

RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential cmake git ca-certificates \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /src
COPY . .

# third_party/llama.cpp is the pinned submodule; the Makefile applies
# patches/llama.cpp-qwen3-coder-any-argument-order.patch if it is not already.
# Its .git pointer names a path outside the context, so drop it: git apply then
# runs in no-repository mode, which is all the patch step needs.
RUN rm -rf third_party/llama.cpp/.git \
    && make build PASCAL=1 CUDA_ARCHS="${CUDA_ARCHS}"


FROM nvidia/cuda:12.8.1-runtime-ubuntu24.04

# ca-certificates/curl are only for the healthcheck.
RUN apt-get update && apt-get install -y --no-install-recommends \
        ca-certificates curl \
    && rm -rf /var/lib/apt/lists/*

# All binaries land in ONE directory: the front end resolves a bare --engine
# name relative to its own path (/proc/self/exe), and the engines link
# libstrata_kq.so from their rpath, so the library sits beside them too.
COPY --from=build /src/build/bin/unbiased-strata /usr/local/bin/unbiased-strata
COPY --from=build /src/build/bin/strata          /usr/local/bin/strata
COPY --from=build /src/build/bin/strata-poly     /usr/local/bin/strata-poly
COPY --from=build /src/build/bin/strata-dsv41    /usr/local/bin/strata-dsv41
COPY --from=build /src/build/bin/libstrata_kq.so /usr/local/bin/libstrata_kq.so

EXPOSE 8080

# /health answers 503 while the engine is still loading; the long start period
# keeps a slow first load (the model is read into RAM and VRAM) from being
# marked unhealthy and restarted mid-load.
HEALTHCHECK --interval=30s --timeout=5s --start-period=1800s --retries=3 \
    CMD curl -fs http://127.0.0.1:8080/health || exit 1

ENTRYPOINT ["/usr/local/bin/unbiased-strata"]
