Providing Dockerized Flask boilerplate with multi-stage builds
I'm preparing a minimal Flask app with requirements and a multi-stage Dockerfile showing two options: one using Alpine that builds wheels then installs them at runtime to minimize attack surface, and another using distroless that vendors installed libs from builder to runtime without pip, fully separating build dependencies. This covers the user's request for multi-stage builds with compiled wheels and minimal runtime images.
Here's a minimal Flask “hello world” wired up with a two‑stage build. The first stage has the compiler toolchain and produces wheels, the second stage only ever sees the compiled artifacts and your app code, and runs on a very small base.
.
├── app.py
├── requirements.txt
└── Dockerfile
app.py:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Hello from Flask in Docker!"
if __name__ == "__main__":
# for local debugging; in the container we'll use gunicorn
app.run(host="0.0.0.0", port=8000)
requirements.txt:
Flask==2.3.2
gunicorn==21.2.0
Dockerfile (Alpine runtime):
# syntax=docker/dockerfile:1
############################
# Builder: compile wheels
############################
FROM python:3.11-alpine AS builder
# Install build deps (C compiler, headers) for any packages that need to compile
RUN apk add --no-cache build-base
WORKDIR /build
COPY requirements.txt .
# Build wheels into /wheels to avoid rebuilding in the runtime image
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /wheels -r requirements.txt
############################
# Runtime: minimal image
############################
FROM python:3.11-alpine AS runtime
# Create an unprivileged user
RUN addgroup -S app && adduser -S app -G app
USER app
WORKDIR /app
# Copy only the wheels and install from them; no compiler needed here
COPY --from=builder /wheels /wheels
COPY requirements.txt .
RUN pip install --no-cache-dir --no-index --find-links=/wheels -r requirements.txt && rm -rf /wheels
# Copy your application code
COPY app.py .
EXPOSE 8000
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]
If you prefer a distroless runtime, vendor the dependencies in the builder and copy them into a distroless base that has no package manager:
# Builder
FROM python:3.11-slim AS builder
RUN apt-get update && apt-get install -y --no-install-recommends build-essential && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY requirements.txt .
# Install into /opt/venv (or /install) so we can copy it wholesale
RUN python -m venv /opt/venv && \
/opt/venv/bin/pip install --no-cache-dir -r requirements.txt
COPY app.py .
# Runtime
FROM gcr.io/distroless/python3-debian12:nonroot
ENV VIRTUAL_ENV=/opt/venv \
PATH="/opt/venv/bin:$PATH"
WORKDIR /app
COPY --from=builder /opt/venv /opt/venv
COPY --from=builder /app/app.py .
# Distroless has no shell, so just exec the module directly
CMD ["app.py"]
With the Alpine version, build and run like this:
docker build -t flask-multistage .
docker run --rm -p 8000:8000 flask-multistage
Both approaches keep the build toolchain out of the final image and only carry the compiled wheels/artifacts into a small runtime to reduce the attack surface.