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.9-alpine3.14
python3.8-alpine3.10
python3.7-alpine3.8
python3.6
python3.6-alpine3.8
The last date tags for these versions are:
python3.9-alpine3.14-2024-03-11
python3.8-alpine3.10-2024-01-29
python3.7-alpine3.8-2024-03-11
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-fastapi:python3.7-2019-10-15
.
uvicorn-gunicorn-fastapi
Docker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python with performance auto-tuning.
GitHub repo: https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker
Docker Hub image: https://hub.docker.com/r/tiangolo/uvicorn-gunicorn-fastapi/
Description
FastAPI has shown to be a Python web framework with one of the best performances, as measured by third-party benchmarks, thanks to being based on and powered by Starlette.
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.
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.
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.
FastAPI
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python.
The key features are:
- Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic).
- Fast to code: Increase the speed to develop features by about 200% to 300% *.
- Less bugs: Reduce about 40% of human (developer) induced errors. *
- Intuitive: Great editor support. Completion everywhere. Less time debugging.
- Easy: Designed to be easy to use and learn. Less time reading docs.
- Short: Minimize code duplication. Multiple features from each parameter declaration. Less bugs.
- Robust: Get production-ready code. With automatic interactive documentation.
- Standards-based: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.
* estimation based on tests on an internal development team, building production applications.
tiangolo/uvicorn-gunicorn-fastapi
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 are also slim versions. If you want one of those, use one of the tags from above.
tiangolo/uvicorn-gunicorn
This image (tiangolo/uvicorn-gunicorn-fastapi
) is based on tiangolo/uvicorn-gunicorn.
That image is what actually does all the work.
This image just installs FastAPI and has the documentation specifically targeted at FastAPI.
If you feel confident about your knowledge of Uvicorn, Gunicorn and ASGI, you can use that image directly.
tiangolo/uvicorn-gunicorn-starlette
There is a sibling Docker image: tiangolo/uvicorn-gunicorn-starlette
If you are creating a new Starlette web application and you want to discard all the additional features from FastAPI you should use tiangolo/uvicorn-gunicorn-starlette 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-fastapi: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 FastAPI application.
Then you can build your image from the directory that has your Dockerfile
, e.g:
docker build -t myimage ./
Quick Start
Build your Image
- Go to your project directory.
- Create a
Dockerfile
with:
FROM tiangolo/uvicorn-gunicorn-fastapi: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 fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
- 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 FastAPI 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 FastAPI server in a Docker container. Auto-tuned for your current server (and number of CPU cores).
Check it
You should be able to check it in your Docker container's URL, for example: http://192.168.99.100/items/5?q=somequery or http://127.0.0.1/items/5?q=somequery (or equivalent, using your Docker host).
You will see something like:
{"item_id": 5, "q": "somequery"}
Interactive API docs
Now you can go to http://192.168.99.100/docs or http://127.0.0.1/docs (or equivalent, using your Docker host).
You will see the automatic interactive API documentation (provided by Swagger UI):
Alternative API docs
And you can also go to http://192.168.99.100/redoc or http://127.0.0.1/redoc(or equivalent, using your Docker host).
You will see the alternative automatic documentation (provided by ReDoc):
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 FastAPI.
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-fastapi: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