Dockerfile & Docker Compose Generator · Tutorial

How to put a Python web app in a Docker container

8 min readbeginnerUpdated

Your Python web app runs on your machine, but deploying it means installing the right Python version and packages everywhere. You will build a container image for it and run it with Redis.

What you need

Steps

  1. Choose Python

    Load the example. It uses python:3.13-slim, installs requirements.txt and starts Gunicorn on port 8000. For Django, change the command to gunicorn --bind 0.0.0.0:8000 mysite.wsgi.

  2. Check the environment settings

    The image sets PYTHONUNBUFFERED=1 so log lines appear immediately in docker logs, and skips writing .pyc files.

  3. Run as a normal user

    Run as a non-root user adds a user called appuser. Make sure your app doesn’t need to write outside its own folder, or create a writable folder for uploads.

  4. Start with Redis

    Save the files and run docker compose up --build. Your app gets REDIS_URL=redis://cache:6379 for caching or background jobs.

    docker compose up --build

Try it yourself

Opens the Dockerfile & Docker Compose Generator with this tutorial's sample loaded.

Try it now

Common mistakes

Using the Flask development server
flask run is not meant for production. Use Gunicorn or another WSGI server.
Copying the virtual environment
.venv is in .dockerignore for a reason: packages are installed fresh inside the image.

Next steps

Go back to the Dockerfile & Docker Compose Generator, or keep going with one of these: