QA
Senior LevelPython

How do you architect ultra-lean 60MB production Docker images for Python using multi-stage builds, virtualenvs, and Astral uv?

❓ Interview Prompt: Explain Docker Multi-Stage Builds, builder stage (compiling wheels with `uv`), runner stage (`gcr.io/distroless/python3`), and non-root execution.

Production Python Docker images should be small, fast to build, and secure:

Multi-Stage Build Architecture:

•
Stage 1 (Builder): Use 'python:3.12-slim', install 'uv', and compile virtual environment dependencies ('uv sync').
•
Stage 2 (Runner): Copy ONLY the compiled '/app/.venv' into a 'gcr.io/distroless/python3' base image running as a non-root user (Produces a tiny 60MB secure production container!).

Code Implementation Example

dockerfile
# Stage 1: Builder
FROM python:3.12-slim AS builder
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
WORKDIR /app
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev

# Stage 2: Runner
FROM gcr.io/distroless/python3-debian12 AS runner
WORKDIR /app
COPY --from=builder /app/.venv /app/.venv
ENV PATH="/app/.venv/bin:$PATH"
USER nonroot
CMD ["python", "-m", "app.main"]
💡Multi-stage Dockerfile producing ultra-lean distroless Python image using uv.
🎯 Interview Key Takeaway

Use Docker multi-stage builds with Astral uv and distroless runner stages to build 60MB production Python containers.

#Docker#Multi-Stage#uv#Distroless#Senior
Back to all Python Questions→

More Python Interview Questions

Beginner LevelPython

What is the Python Global Interpreter Lock (GIL) and how does it affect multi-threading?

Beginner LevelPython

What is the difference between a List, Tuple, Set, and Dictionary in Python?

Beginner LevelPython

Why are mutable default arguments (like def func(items=[])) dangerous in Python and how do you fix them?