This Docker image provides a platform to run Python applications with Uvicorn on Kubernetes container orchestration system. It provides Poetry for managing dependencies and setting up a virtual environment in the container.
This image aims to follow the best practices for a production grade container image for hosting Python web applications based on micro frameworks like FastAPI. Therefore, source and documentation contain a lot of references to documentation of dependencies used in this project, so users of this image can follow up on that.
Any feedback is highly appreciated and will be considered.
Docker Hub: pfeiffermax/uvicorn-poetry
GitHub Repository: https://github.com/max-pfeiffer/uvicorn-poetry
- Poetry v1.7.1 is available as Python package dependency management tool
- A virtual environment for the application and application server
- The application is run with Uvicorn as application server
- Python versions:
- 3.10
- 3.11
- 3.12
- Operating system variants:
- Supported CPU architectures:
- linux/amd64
- linux/arm64/v8
The image provides a platform to run your Python application, so it does not provide an application itself.
Please have a look at the single stage and multi stage example to learn how to use the image.
The multi stage approach is a bit more efficient with regard to build time. It caches the Python package dependencies in a separate build stage.
You can also use the uvicorn-poetry-fastapi-project-template for your convenience (requires Cookiecutter). The generated project basically contains the Dockerfile of this image and production image is build upon the standard Python image which results in an even smaller image size eventually.
Please be aware that your application needs an application layout without src folder which is proposed in fastapi-realworld-example-app. The application and test structure needs to be like that:
├── .dockerignore
├── Dockerfile
├── app
│ ├── __init__.py
│ └── main.py
├── poetry.lock
├── pyproject.toml
└── tests
├── __init__.py
├── conftest.py
└── test_api
├── __init__.py
├── test_items.py
└── test_root.py
Please be aware that you need to provide a pyproject.toml file to specify your Python package dependencies for Poetry and configure dependencies like Pytest. Poetry dependencies must at least contain the following to work:
- python = "^3.11"
- uvicorn = "0.24.0"
If your application uses FastAPI framework this needs to be added as well:
- fastapi = "0.104.1"
IMPORTANT: make sure you have a .dockerignore file in your application root which excludes your local virtual environment in .venv! Otherwise you will have an issue activating that virtual environment when running the container.
Configuration is done through command line options and arguments in the
Dockerfile.
For everything else Uvicorn uses its defaults.
Since Uvicorn v0.16.0 you can configure Uvicorn via
environment variables with the prefix UVICORN_
.
If you would like to do a deep dive on all the configuration options please see the
official Uvicorn documentation.
- Scripts for entrypoints are dropped and removed
- Application is run with an unprivileged user
These custom environment variables are not supported anymore:
LOG_LEVEL
: The granularity of Error log outputs.LOG_CONFIG_FILE
: Logging configuration file.RELOAD
: Enable auto-reload.