# syntax=docker/dockerfile:1.7
#
# Keyladder container image.
#
#   docker build -t keyladder:local .
#   docker run --rm -p 127.0.0.1:8787:8787 -e KEYLADDER_CONTAINER_LOCAL=true -v keyladder-data:/data keyladder:local
#
# Base images: the defaults below are pinned by tag, which is what local builds, compose and the CI
# smoke test use. The release workflow (.github/workflows/release.yml) overrides all three build args
# with digest-pinned references (image:tag@sha256:...), so a published image is built from exactly the
# bases reviewed in that file. To reproduce a release build locally, pass the same values:
#   docker build --build-arg PYTHON_IMAGE=python:3.12-slim-bookworm@sha256:<digest> ... .
# How to refresh the digests: docs/release.md ("Base images").
# No secrets are baked in: the master key is generated in the /data volume on first start (or passed via
# KEYLADDER_MASTER_KEY at run time), provider keys are added through the dashboard or `keyladder apply`.

ARG NODE_IMAGE=node:22.12-bookworm-slim
ARG PYTHON_IMAGE=python:3.12-slim-bookworm
ARG UV_IMAGE=ghcr.io/astral-sh/uv:0.11.32

FROM ${UV_IMAGE} AS uv

# --------------------------------------------------------------------------- stage 1: dashboard
FROM ${NODE_IMAGE} AS web
WORKDIR /build/web
COPY web/package.json web/package-lock.json ./
RUN npm ci --no-audit --no-fund
COPY web/ ./
# vite.config.ts writes the bundle to ../src/keyladder/static/app
RUN npm run build

# --------------------------------------------------------------------------- stage 2: python environment
FROM ${PYTHON_IMAGE} AS builder
COPY --from=uv /uv /usr/local/bin/uv
ENV UV_COMPILE_BYTECODE=1 \
    UV_LINK_MODE=copy \
    UV_PYTHON_DOWNLOADS=never \
    UV_PROJECT_ENVIRONMENT=/opt/keyladder
WORKDIR /src
# Dependencies first (cached unless the lock changes).
COPY pyproject.toml uv.lock README.md ./
RUN uv sync --locked --no-dev --extra postgres --no-install-project
# Then the project itself, with the built dashboard, installed non-editable into /opt/keyladder.
COPY src/ ./src/
COPY --from=web /build/src/keyladder/static/app/ ./src/keyladder/static/app/
RUN uv sync --locked --no-dev --extra postgres --no-editable \
    && /opt/keyladder/bin/python -c "import keyladder, pathlib; \
p = pathlib.Path(keyladder.__file__).parent / 'static' / 'app' / 'index.html'; \
assert p.is_file(), 'dashboard bundle missing from the installed package'"

# --------------------------------------------------------------------------- runtime
FROM ${PYTHON_IMAGE} AS runtime
LABEL org.opencontainers.image.title="Keyladder" \
      org.opencontainers.image.description="Local-first LLM gateway" \
      org.opencontainers.image.licenses="Apache-2.0"
# Debian security fixes published after the base image was built (the release's Trivy scan fails on any
# fixable HIGH/CRITICAL vulnerability). No new packages are installed.
RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get upgrade -y --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*
RUN groupadd --system --gid 10001 keyladder \
    && useradd --system --uid 10001 --gid keyladder --home-dir /data --no-create-home \
       --shell /usr/sbin/nologin keyladder \
    && mkdir -p /data \
    && chown keyladder:keyladder /data \
    && chmod 0700 /data
COPY --from=builder /opt/keyladder /opt/keyladder
# Git commit of this build, shown by GET /version: docker build --build-arg KEYLADDER_BUILD_SHA=$(git rev-parse HEAD)
ARG KEYLADDER_BUILD_SHA=""
ENV KEYLADDER_BUILD_SHA=${KEYLADDER_BUILD_SHA}
ENV PATH=/opt/keyladder/bin:$PATH \
    PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    KEYLADDER_DATA_DIR=/data \
    KEYLADDER_HOST=0.0.0.0 \
    KEYLADDER_PORT=8787 \
    KEYLADDER_ALLOW_REMOTE=true
# KEYLADDER_ALLOW_REMOTE=true only permits the 0.0.0.0 bind *inside* the container. Publish the port on
# host loopback (-p 127.0.0.1:8787:8787) and set KEYLADDER_CONTAINER_LOCAL=true for http://localhost
# sign-in; see docs/deployment.md.
USER keyladder:keyladder
WORKDIR /data
VOLUME ["/data"]
EXPOSE 8787
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
    CMD ["python", "-c", "import os, sys, urllib.request; port = os.environ.get('KEYLADDER_PORT', '8787'); sys.exit(0 if urllib.request.urlopen(f'http://127.0.0.1:{port}/health/live', timeout=4).status == 200 else 1)"]
ENTRYPOINT ["keyladder"]
# `serve` runs database migrations on start (KEYLADDER_AUTO_MIGRATE=true by default).
CMD ["serve", "--host", "0.0.0.0"]
