# Étape 1 : build de llama.cpp
FROM ubuntu:24.04 AS builder

LABEL maintainer="vous" \
      description="llama.cpp + Mistral-7B-Instruct (inférence locale isolée)"

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends \
      lsb-release           \
      gnupg                 \
      ca-certificates       \
      build-essential       \
      cmake                 \
      git                   \
      libopenblas-dev       \
      pkg-config            \
      libvulkan-dev         \
      vulkan-tools          \
      wget

# Télécharge et installe le Vulkan SDK
RUN wget -qO - https://packages.lunarg.com/lunarg-signing-key-pub.asc | apt-key add - && \
    echo "deb https://packages.lunarg.com/vulkan/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/lunarg-vulkan.list && \
    apt-get update && \
    apt-get install -y vulkan-sdk && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /build
RUN git clone --depth 1 https://github.com/ggerganov/llama.cpp .

RUN cmake -B build \
       -DLLAMA_BUILD_TESTS=OFF \
       -DLLAMA_BUILD_EXAMPLES=ON \
       -DGGML_BLAS=ON \
       -DGGML_BLAS_VENDOR=OpenBLAS \
       -DCMAKE_INSTALL_PREFIX=/install \
       -DGGML_VULKAN=ON \
       -DVulkan_GLSLC_EXECUTABLE=/usr/bin/glslc \
    && cmake --build build --config Release -j$(nproc) \
    && cmake --install build --prefix /install

# Étape 2 : image finale (runtime minimal)
FROM ubuntu:24.04 AS runtime

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends \
      libopenblas0-openmp   \
      libgomp1              \
      libvulkan1            \
      mesa-vulkan-drivers   \
    && rm -rf /var/lib/apt/lists/*

COPY --from=builder /install/ /usr/local/
RUN ldconfig

RUN useradd -m -u 1001 -s /bin/bash llama
USER llama

WORKDIR /home/llama
RUN mkdir -p /home/llama/models

EXPOSE 8080

CMD ["llama-server", \
     "--model",   "/home/llama/models/mistral-7b-instruct.gguf", \
     "--host",    "0.0.0.0", \
     "--port",    "8080", \
     "-c",        "4096", \
     "-ngl",      "33"]