Skip to content

Commit 7ef624f

Browse files
committed
feat(docker): reduce image size from 351MB to 150MB via multi-stage Bookworm build
- Implements multi-stage build to exclude build tools - Maintains Debian Trixie base for glibc compatibility (Pillow support) - Adds non-root user for security (referenced from nasa#146) - Integrates uv with Docker cache mounts for faster builds
1 parent cc32230 commit 7ef624f

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

Dockerfile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# --- STAGE 1: Builder ---
22
# We use a specific SHA to ensure the build is deterministic and reproducible.
3-
FROM python:3.12.10-alpine3.22@sha256:4bbf5ef9ce4b273299d394de268ad6018e10a9375d7efc7c2ce9501a6eb6b86c AS builder
3+
FROM python:3.12.10-slim-bookworm@sha256:fd95fa221297a88e1cf49c55ec1828edd7c5a428187e67b5d1805692d11588db AS builder
44

55
# Install uv (extremely fast Python package manager) by copying the binary from the official image
66
COPY --from=ghcr.io/astral-sh/uv:0.9.26 /uv /bin/uv
@@ -37,12 +37,13 @@ RUN --mount=type=cache,target=/root/.cache/uv \
3737

3838
# --- STAGE 2: Runtime ---
3939
# We use the exact same base image to ensure binary compatibility with Alpine's musl C library.
40-
FROM python:3.12.10-alpine3.22@sha256:4bbf5ef9ce4b273299d394de268ad6018e10a9375d7efc7c2ce9501a6eb6b86c
40+
FROM python:3.12.10-slim-bookworm@sha256:fd95fa221297a88e1cf49c55ec1828edd7c5a428187e67b5d1805692d11588db
4141

4242
# Security: Create a non-privileged user to run the application.
4343
# Alpine uses 'addgroup' and 'adduser' syntax.
44-
RUN addgroup -S -g 1000 nonroot && \
45-
adduser -S -u 1000 -G nonroot nonroot
44+
# Setup a non-root user
45+
RUN groupadd --system --gid 1000 nonroot \
46+
&& useradd --system --gid 1000 --uid 1000 --create-home nonroot
4647

4748
# Copy only the necessary files from the builder (app + .venv).
4849
# This keeps the final image size small (~81MB vs ~351MB).

0 commit comments

Comments
 (0)