# Stage 1: Build Ollama with Vulkan support
FROM ubuntu:24.04 AS builder

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y \
    build-essential \
    cmake \
    git \
    curl \
    golang-go \
    libvulkan-dev \
    vulkan-tools \
    glslang-tools \
    ninja-build \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app
RUN git clone --recursive https://github.com/ollama/ollama.git .

# Use Ollama's official preset configuration for Vulkan compilation
RUN cmake -S llama/server --preset vulkan
RUN cmake --build build/llama-server-vulkan --parallel $(nproc)
RUN cmake --install build/llama-server-vulkan --component llama-server --prefix /app/dist

# Build the main Go application binary
RUN go build -o ollama .

# Stage 2: Minimal Runtime Image
FROM ubuntu:24.04

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y \
    libvulkan1 \
    mesa-vulkan-drivers \
    vulkan-tools \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Copy the compiled Go binary and the Vulkan backend runtime payload
COPY --from=builder /app/ollama /usr/local/bin/ollama
COPY --from=builder /app/dist /usr/local

EXPOSE 11434
ENV OLLAMA_HOST=0.0.0.0:11434
ENV OLLAMA_MODELS=/root/.ollama/models

ENTRYPOINT ["ollama", "serve"]