Home / Configs / Containers / Dockerfile for Python

Dockerfile for Python: Flask, Django and FastAPI example

A working Dockerfile for a Python web app, with Gunicorn, Uvicorn or a plain script.

For Docker 20 or newer and a Python app with a requirements.txt file.

This Dockerfile installs your packages from requirements.txt and starts your app with the server you choose. Fill in the details below, save it in your project folder, then build and run it.

Create your config file

Type your own values. The file below updates as you type, and nothing you type leaves your browser.

Match the version you use on your own machine
For Gunicorn or Uvicorn use file:object, like main:app. For a script use the file name, like app.py
The port the server listens on

Dockerfile
FROM python:3.13-slim
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN useradd --create-home appuser
USER appuser
EXPOSE 8000
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "app:app"]

How this setup works

Diagram: Your code, then docker build, then Container
Your code (requirements.txt and app) → docker build (installs packages) → Container (runs on your port)

Step-by-step setup

  1. Save the file above as Dockerfile in your project folder, next to requirements.txt.
  2. Make sure requirements.txt lists your packages, including the server you chose (gunicorn or uvicorn).
  3. Build the image: docker build -t myapp .
  4. Run it: docker run -p 8000:8000 myapp.
  5. Open http://localhost:8000 in your browser.

What the important lines mean

Common errors and how to fix them

ModuleNotFoundError: No module named 'gunicorn'
The server is not listed in requirements.txt. Add gunicorn (or uvicorn) to that file and build again.
ERROR: Could not open requirements file
requirements.txt is missing or not in the same folder as the Dockerfile. Create it with pip freeze > requirements.txt.
Failed to find attribute 'app' in 'app'
The module setting is wrong. Use the file name, a colon, then the variable that holds your app, like main:app.
The container runs but the browser cannot connect
The server must bind to 0.0.0.0 and you must publish the port with -p 8000:8000.
PermissionError when the app writes a file
The non-root user cannot write to /app. Write to a mounted volume or /tmp, or untick the non-root option.

Frequently asked questions

Which server should I choose?
Use Gunicorn for Flask and Django, and Uvicorn for FastAPI. Use the plain script option only for small tools and scripts.
Why slim and not Alpine?
Many Python packages ship ready-made builds for Debian-based images. On Alpine they often have to compile, which is slow and can fail.
Do I need a .dockerignore file?
It helps. List .git, __pycache__ and your virtual environment folder so they are not copied into the image.

Written for: Docker 24 or newer, Python 3.11 to 3.13 · Updated 2026-10-11

Free tools that help

Fix a problem with this setup

More container configs

See all 41 config generators