# CPU-only development candidate. No model is bundled or downloaded.
# Every x86-64 CPU tier is built and the best one is loaded at startup, as
# upstream's cpu.Dockerfile does: a single AVX2 build dies with SIGILL on
# nodes (and VMs) without AVX.
# Base image pinned by multi-arch index digest (amd64 and arm64 included).
FROM docker.io/ubuntu:24.04@sha256:68ee22004a9109b44973313b02ff7b1de250f1de0f5ecb628d73f579aaff33ac AS build
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates git cmake make gcc-14 g++-14 \
    && rm -rf /var/lib/apt/lists/*
ENV CC=gcc-14 CXX=g++-14
WORKDIR /src
# llama.cpp b9860, immutable source revision.
RUN git init . \
    && git remote add origin https://github.com/ggml-org/llama.cpp.git \
    && git fetch --depth 1 origin fdb1db877c526ec90f668eca1b858da5dba85560 \
    && git checkout --detach FETCH_HEAD \
    && test "$(git rev-parse HEAD)" = fdb1db877c526ec90f668eca1b858da5dba85560 \
    && cmake -S . -B build -DCMAKE_BUILD_TYPE=Release \
       -DGGML_NATIVE=OFF -DGGML_BACKEND_DL=ON -DGGML_CPU_ALL_VARIANTS=ON \
       -DLLAMA_BUILD_TESTS=OFF -DLLAMA_BUILD_UI=OFF -DLLAMA_USE_PREBUILT_UI=OFF \
       -DLLAMA_OPENSSL=OFF \
    && cmake --build build -j2 --target llama-server \
       $(cmake --build build --target help | grep -o 'ggml-cpu-[a-z0-9_]*' | sort -u) \
    && mkdir /out && cp build/bin/llama-server /out/ \
    && find build -name '*.so*' -exec cp -P {} /out/ \;

FROM docker.io/ubuntu:24.04@sha256:68ee22004a9109b44973313b02ff7b1de250f1de0f5ecb628d73f579aaff33ac
RUN apt-get update && apt-get install -y --no-install-recommends libgomp1 libstdc++6 curl \
    && rm -rf /var/lib/apt/lists/*
# Backends are discovered beside the executable.
COPY --from=build /out/ /opt/llama/
ENV LLAMA_ARG_HOST=0.0.0.0 LLAMA_ARG_PORT=8080 LD_LIBRARY_PATH=/opt/llama
WORKDIR /tmp
EXPOSE 8080
ENTRYPOINT ["/opt/llama/llama-server"]
