Supported tags and respective Dockerfile
links
python3.11
,latest
(Dockerfile)python3.10
, (Dockerfile)python3.9
, (Dockerfile)python3.8
, (Dockerfile)python3.7
, (Dockerfile)python3.11-slim
(Dockerfile)python3.10-slim
(Dockerfile)python3.9-slim
(Dockerfile)python3.8-slim
(Dockerfile)
Deprecated tags
🚨 These tags are no longer supported or maintained, they are removed from the GitHub repository, but the last versions pushed might still be available in Docker Hub if anyone has been pulling them:
python3.8-alpine3.10
python3.9-alpine3.14
python3.7-alpine3.8
python3.6
python3.6-alpine3.8
The last date tags for these versions are:
python3.8-alpine3.10-2024-03-17
python3.9-alpine3.14-2024-03-17
python3.7-alpine3.8-2024-03-17
python3.6-2022-11-25
python3.6-alpine3.8-2022-11-25
Note: There are tags for each build date. If you need to "pin" the Docker image version you use, you can select one of those tags. E.g. tiangolo/uvicorn-gunicorn-starlette:python3.7-2019-10-15
.
uvicorn-gunicorn-starlette
Docker image with Uvicorn managed by Gunicorn for high-performance Starlette web applications in Python with performance auto-tuning.
GitHub repo: https://github.com/tiangolo/uvicorn-gunicorn-starlette-docker
Docker Hub image: https://hub.docker.com/r/tiangolo/uvicorn-gunicorn-starlette/
Description
Starlette has shown to be a Python web framework with one of the best performances, as measured by third-party benchmarks.
The achievable performance is on par with (and in many cases superior to) Go and Node.js frameworks.
This image has an auto-tuning mechanism included to start a number of worker processes based on the available CPU cores. That way you can just add your code and get high performance automatically, which is useful in simple deployments.
🚨 WARNING: You Probably Don't Need this Docker Image
You are probably using Kubernetes or similar tools. In that case, you probably don't need this image (or any other similar base image). You are probably better off building a Docker image from scratch as explained in the docs for FastAPI in Containers - Docker: Build a Docker Image for FastAPI, the same process could be applied to Starlette.
If you have a cluster of machines with Kubernetes, Docker Swarm Mode, Nomad, or other similar complex system to manage distributed containers on multiple machines, then you will probably want to handle replication at the cluster level instead of using a process manager (like Gunicorn with Uvicorn workers) in each container, which is what this Docker image does.
In those cases (e.g. using Kubernetes) you would probably want to build a Docker image from scratch, installing your dependencies, and running a single Uvicorn process instead of this image.
For example, your Dockerfile
could look like:
FROM python:3.9
WORKDIR /code
COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
COPY ./app /code/app
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
You can read more about this in the FastAPI documentation about: FastAPI in Containers - Docker as the same ideas would apply to Starlette.
When to Use this Docker Image
A Simple App
You could want a process manager like Gunicorn running Uvicorn workers in the container if your application is simple enough that you don't need (at least not yet) to fine-tune the number of processes too much, and you can just use an automated default, and you are running it on a single server, not a cluster.
Docker Compose
You could be deploying to a single server (not a cluster) with Docker Compose, so you wouldn't have an easy way to manage replication of containers (with Docker Compose) while preserving the shared network and load balancing.
Then you could want to have a single container with a Gunicorn process manager starting several Uvicorn worker processes inside, as this Docker image does.
Prometheus and Other Reasons
You could also have other reasons that would make it easier to have a single container with multiple processes instead of having multiple containers with a single process in each of them.
For example (depending on your setup) you could have some tool like a Prometheus exporter in the same container that should have access to each of the requests that come.
In this case, if you had multiple containers, by default, when Prometheus came to read the metrics, it would get the ones for a single container each time (for the container that handled that particular request), instead of getting the accumulated metrics for all the replicated containers.
Then, in that case, it could be simpler to have one container with multiple processes, and a local tool (e.g. a Prometheus exporter) on the same container collecting Prometheus metrics for all the internal processes and exposing those metrics on that single container.
Read more about it all in the FastAPI documentation about: FastAPI in Containers - Docker.
Technical Details
Uvicorn
Uvicorn is a lightning-fast "ASGI" server.
It runs asynchronous Python web code in a single process.
Gunicorn
You can use Gunicorn to start and manage multiple Uvicorn worker processes.
That way, you get the best of concurrency and parallelism in simple deployments.
Starlette
Starlette is a lightweight ASGI framework/toolkit, which is ideal for building high performance asyncio services.
tiangolo/uvicorn-gunicorn-starlette
This image will set a sensible configuration based on the server it is running on (the amount of CPU cores available) without making sacrifices.
It has sensible defaults, but you can configure it with environment variables or override the configuration files.
There is also a slim version. If you want that, use one of the tags from above.
tiangolo/uvicorn-gunicorn
This image (tiangolo/uvicorn-gunicorn-starlette
) is based on tiangolo/uvicorn-gunicorn.
That image is what actually does all the work.
This image just installs Starlette and has the documentation specifically targeted at Starlette.
If you feel confident about your knowledge of Uvicorn, Gunicorn and ASGI, you can use that image directly.
tiangolo/uvicorn-gunicorn-fastapi
There is a sibling Docker image: tiangolo/uvicorn-gunicorn-fastapi
If you are creating a new FastAPI web application you should use tiangolo/uvicorn-gunicorn-fastapi instead.
Note: FastAPI is based on Starlette and adds several features on top of it. Useful for APIs and other cases: data validation, data conversion, documentation with OpenAPI, dependency injection, security/authentication and others.
How to use
You don't need to clone the GitHub repo.
You can use this image as a base image for other images.
Assuming you have a file requirements.txt
, you could have a Dockerfile
like this:
FROM tiangolo/uvicorn-gunicorn-starlette:python3.11
COPY ./requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
COPY ./app /app
It will expect a file at /app/app/main.py
.
Or otherwise a file at /app/main.py
.
And will expect it to contain a variable app
with your Starlette application.
Then you can build your image from the directory that has your Dockerfile
, e.g:
docker build -t myimage ./
Quick Start
- Go to your project directory.
- Create a
Dockerfile
with:
FROM tiangolo/uvicorn-gunicorn-starlette:python3.11
COPY ./requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
COPY ./app /app
- Create an
app
directory and enter in it. - Create a
main.py
file with:
from starlette.applications import Starlette
from starlette.responses import JSONResponse
app = Starlette()
@app.route("/")
async def homepage(request):
return JSONResponse({"message": "Hello World!"})
- You should now have a directory structure like:
.
├── app
│ └── main.py
└── Dockerfile
- Go to the project directory (in where your
Dockerfile
is, containing yourapp
directory). - Build your Starlette image:
docker build -t myimage .
- Run a container based on your image:
docker run -d --name mycontainer -p 80:80 myimage
Now you have an optimized Starlette server in a Docker container. Auto-tuned for your current server (and number of CPU cores).
You should be able to check it in your Docker container's URL, for example: http://192.168.99.100/ or http://127.0.0.1/ (or equivalent, using your Docker host).
You will see something like:
{"message": "Hello World!"}
Dependencies and packages
You will probably also want to add any dependencies for your app and pin them to a specific version, probably including Uvicorn, Gunicorn, and Starlette.
This way you can make sure your app always works as expected.
You could install packages with pip
commands in your Dockerfile
, using a requirements.txt
, or even using Poetry.
And then you can upgrade those dependencies in a controlled way, running your tests, making sure that everything works, but without breaking your production application if some new version is not compatible.
Using Poetry
Here's a small example of one of the ways you could install your dependencies making sure you have a pinned version for each package.
Let's say you have a project managed with Poetry, so, you have your package dependencies in a file pyproject.toml
. And possibly a file poetry.lock
.
Then you could have a Dockerfile
using Docker multi-stage building with:
FROM python:3.9 as requirements-stage
WORKDIR /tmp
RUN pip install poetry
COPY ./pyproject.toml ./poetry.lock* /tmp/
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes
FROM tiangolo/uvicorn-gunicorn-starlette:python3.11
COPY --from=requirements-stage /tmp/requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
COPY ./app /app
That will:
- Install poetry and configure it for running inside of the Docker container.
- Copy your application requirements.
- Because it uses
./poetry.lock*
(ending with a*
), it won't crash if that file is not available yet.
- Because it uses
- Install the dependencies.
- Then copy your app code.
It's important to copy the app code after installing the dependencies, that way you can take advantage of Docker's cache. That way it won't have to install everything from scratch every time you update your application files, only when you add new dependencies.
This also applies for any other way you use to install your dependencies. If you use a requirements.txt
, copy it alone and install all the dependencies on the top of the Dockerfile
, and add your app code after it.
Advanced usage
Environment variables
These are the environment variables that you can set in the container to configure it and their default values:
MODULE_NAME
The Python "module" (file) to be imported by Gunicorn, this module would contain the actual application in a variable.
By default:
app.main
if there's a file/app/app/main.py
ormain
if there's a file/app/main.py
For example, if your main file was at /app/custom_app/custom_main.py
, you could set it like:
docker run -d -p 80:80 -e MODULE_NAME="custom_app.custom_main" myimage
VARIABLE_NAME
The variable inside of the Python module that contains the Starlette application.
By default:
app
For example, if your main Python file has something like:
from starlette.applications import Starlette
from starlette.responses import JSONResponse
api = Starlette()
@api.route("/")
async def homepage(request):
return JSONResponse({"message": "Hello World!"})
In this case api
would be the variable with the Starlette application. You could set it like:
docker run -d -p 80:80 -e VARIABLE_NAME="api" myimage
APP_MODULE
The string with the Python module and the variable name passed to Gunicorn.
By default, set based on the variables MODULE_NAME
and VARIABLE_NAME
:
app.main:app
ormain:app
You can set it like:
docker run -d -p 80:80 -e APP_MODULE="custom_app.custom_main:api" myimage
GUNICORN_CONF
The path to a Gunicorn Python configuration file.
By default:
/app/gunicorn_conf.py
if it exists/app/app/gunicorn_conf.py
if it exists/gunicorn_conf.py
(the included default)
You can set it like:
docker run -d -p 80:80 -e GUNICORN_CONF="/app/custom_gunicorn_conf.py" myimage
You can use the config file from the base image as a starting point for yours.
WORKERS_PER_CORE
This image will check how many CPU cores are available in the current server running your container.
It will set the number of workers to the number of CPU cores multiplied by this value.
By default:
1
You can set it like:
docker run -d -p 80:80 -e WORKERS_PER_CORE="3" myimage
If you used the value 3
in a server with 2 CPU cores, it would run 6 worker processes.
You can use floating point values too.
So, for example, if you have a big server (let's say, with