11# Multi-stage build: keep the runtime image lean.
22
33# ---------------------------------------------------------------------------
4- # Stage 1 – builder: install dependencies and bake the model weights.
4+ # Stage 1 – builder: install dependencies only.
5+ # Targeted by CI (target: builder) to verify the build without downloading
6+ # the 300 MB model weights.
57# ---------------------------------------------------------------------------
68FROM python:3.13-slim@sha256:739e7213785e88c0f702dcdc12c0973afcbd606dbf021a589cab77d6b00b579d AS builder
79
@@ -18,12 +20,17 @@ WORKDIR /app
1820
1921# Install dependencies only (no source code) so layer cache is reused when
2022# only application code changes.
21- COPY pyproject.toml uv.lock ./
23+ COPY pyproject.toml uv.lock README.md ./
2224RUN uv sync --frozen --no-dev
2325
26+ # ---------------------------------------------------------------------------
27+ # Stage 2 – baked: add model weights on top of the installed dependencies.
2428# Bake ArcFace + RetinaFace model weights into the image at build time.
2529# The resulting image starts instantly and works in airgapped environments.
2630# Model updates require an image rebuild.
31+ # ---------------------------------------------------------------------------
32+ FROM builder AS baked
33+
2734RUN DEEPFACE_HOME=/root/.deepface uv run python -c \
2835 "from deepface import DeepFace; \
2936 import numpy as np; \
@@ -36,15 +43,15 @@ RUN DEEPFACE_HOME=/root/.deepface uv run python -c \
3643 print('ArcFace + RetinaFace models downloaded.')"
3744
3845# ---------------------------------------------------------------------------
39- # Stage 2 – runtime: copy only what's needed to run.
46+ # Stage 3 – runtime: copy only what's needed to run.
4047# ---------------------------------------------------------------------------
4148FROM python:3.13-slim@sha256:739e7213785e88c0f702dcdc12c0973afcbd606dbf021a589cab77d6b00b579d AS runtime
4249
4350WORKDIR /app
4451
45- # Copy the pre-built virtualenv and baked model weights from the builder stage.
46- COPY --from=builder /app/.venv /app/.venv
47- COPY --from=builder /root/.deepface /root/.deepface
52+ # Copy the pre-built virtualenv and baked model weights from the baked stage.
53+ COPY --from=baked /app/.venv /app/.venv
54+ COPY --from=baked /root/.deepface /root/.deepface
4855
4956# Copy application source.
5057COPY app/ ./app/
0 commit comments