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

ENV DEBIAN_FRONTEND=noninteractive

RUN apt update && apt upgrade -y && apt install -y \
    build-essential \
    git \
    curl \
    wget \
    golang-go \
    libvulkan-dev \
    vulkan-tools \
    glslang-tools \
    ninja-build \
    spirv-headers \
    spirv-tools \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Directly install glslc via Debian arm64 package
RUN wget http://ftp.debian.org/debian/pool/main/s/shaderc/glslc_2023.2-1_arm64.deb \
    && apt-get update && apt-get install -y ./glslc_2023.2-1_arm64.deb \
    && rm glslc_2023.2-1_arm64.deb && rm -rf /var/lib/apt/lists/*

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

# Configure and safely build with constrained parallelism to protect 96GB RAM
RUN cmake -B build . -DOLLAMA_LLAMA_BACKENDS="vulkan" -DCMAKE_BUILD_TYPE=Release
RUN cmake --build build --parallel 16

# 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 generated native backend libraries
COPY --from=builder /app/ollama /usr/local/bin/ollama
COPY --from=builder /app/build/lib/ollama /usr/local/lib/ollama

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

ENTRYPOINT ["ollama", "serve"]