Ravi Kant Logo
Ravi Kant
Engineering HubdockerDocker Multi-Stage Production Builds

Docker Multi-Stage Production Builds

Reducing container sizes from 1.2GB to under 45MB with Alpine/Distroless images and multi-stage Dockerfiles.

Docker Multi-Stage Production Builds

Smaller container images mean faster deployment pipelines, reduced attack surfaces, and lower container registry storage costs.

FastAPI Multi-Stage Dockerfile

# Stage 1: Build Dependencies
FROM python:3.12-slim AS builder
WORKDIR /app
RUN pip install poetry
COPY pyproject.toml poetry.lock ./
RUN poetry export -f requirements.txt --output requirements.txt

# Stage 2: Runtime Container
FROM python:3.12-slim-bookworm AS runner
WORKDIR /app
COPY --from=builder /app/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY ./app ./app
EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]