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
- The Dockerfile Generator
- Docker
- A Python app with a requirements.txt that includes gunicorn
Steps
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 togunicorn --bind 0.0.0.0:8000 mysite.wsgi.Check the environment settings
The image sets
PYTHONUNBUFFERED=1so log lines appear immediately indocker logs, and skips writing .pyc files.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.Start with Redis
Save the files and run
docker compose up --build. Your app getsREDIS_URL=redis://cache:6379for caching or background jobs.docker compose up --build
Try it yourself
Opens the Dockerfile & Docker Compose Generator with this tutorial's sample loaded.
Common mistakes
- Using the Flask development server
flask runis not meant for production. Use Gunicorn or another WSGI server.- Copying the virtual environment
.venvis 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: