Dockerfile Builder for Python

Generate a lean Dockerfile for Flask, FastAPI, or Django apps

Build a multi-stage Dockerfile for Python web apps. Picks Python version, framework (FastAPI, Flask, Django), port, a /health HEALTHCHECK and ENTRYPOINT, installs from requirements.txt and runs as a non-root user.

Why build wheels in a separate stage?

The builder stage compiles every dependency into wheels, then the runtime stage installs them with --no-index. This keeps the C compilers and build headers out of the final image while still supporting packages that need compilation.

A builder for a lean, multi-stage Dockerfile for Python web apps — FastAPI, Flask or Django. Choose your Python version, framework and port, and the tool emits an image that builds dependency wheels in one stage and ships a slim runtime that runs as a non-root user.

How it works

The generated Dockerfile uses two stages. The builder stage runs pip wheel to compile every dependency from requirements.txt into a /wheels directory, keeping compilers and headers out of the final image. The runtime stage installs those pre-built wheels with pip install --no-index --find-links=/wheels, sets PYTHONUNBUFFERED=1 and PYTHONDONTWRITEBYTECODE=1, creates a dedicated non-root app user, copies your code, exposes the port and defines an ENTRYPOINT.

Framework start commands

FastAPI is served with uvicorn app.main:app --host 0.0.0.0, Flask with gunicorn --bind 0.0.0.0:PORT app:app, and Django with gunicorn --bind 0.0.0.0:PORT myproject.wsgi:application. Override the start command if your module path differs. The optional HEALTHCHECK calls your /health endpoint using the standard-library urllib, so it adds no extra dependency.

Tips

Keep dependencies in a pinned requirements.txt so builds are reproducible. Add a .dockerignore so caches and secrets are not baked into the image:

__pycache__
*.pyc
.venv
.git
.env

The Debian-based slim base image is chosen deliberately: it works with the prebuilt manylinux wheels most packages ship, avoiding the slow source builds and musl-libc issues common with alpine.