百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
U

uvicorn-gunicorn-starlette-docker

> DevOps
开源

Docker 图像由 Gunicorn 管理,用于在 Python 中运行高性能的 Starlette Web 应用程序,并具有性能自动调整功能。

200 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Docker 图像由 Gunicorn 管理,用于在 Python 中运行高性能的 Starlette Web 应用程序,并具有性能自动调整功能。

DEPRECATED

This Docker image is now deprecated. There's no need to use it, you can just use Uvicorn with --workers. ✨

Read more about it below.


Supported tags and respective Dockerfile links

  • python3.11, latest (Dockerfile)
  • python3.10, (Dockerfile)
  • python3.11-slim (Dockerfile)
  • python3.10-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
  • python3.9-slim
  • python-3.8
  • python-3.8-slim
  • python3.8-alpine3.10
  • python3.9-alpine3.14
  • python3.7
  • python3.7-alpine3.8
  • python3.6
  • python3.6-alpine3.8

The last date tags for these versions are:

  • python3.9-2025-11-09
  • python3.9-slim-2025-11-09
  • python-3.8-2024-11-02
  • python-3.8-slim-2024-11-02
  • python3.8-alpine3.10-2024-03-17
  • python3.9-alpine3.14-2024-03-17
  • python3.7-2024-11-02
  • 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.11-2024-11-02.

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.

Cluster Replication

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:

dockerfile
FROM python:3.11

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.

Multiple Workers

If you definitely want to have multiple workers on a single container, Uvicorn now supports handling subprocesses, including restarting dead ones. So there's no need for Gunicorn to manage multiple workers in a single container.

You could modify the example Dockerfile from above, adding the --workers option to Uvicorn, like:

dockerfile
FROM python:3.11

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", "--workers", "4"]

That's all you need. You don't need this Docker image at all.

You can read more about it in the FastAPI Docs about Deployment with Docker.

Technical Details

Uvicorn didn't have support for managing worker processing including restarting dead workers. But now it does.

Before that, Gunicorn could be used as a process manager, running Uvicorn workers. This added complexity that is no longer necessary.

Legacy Docs

The rest of this document is kept for historical reasons, but you probably don't need it.

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:

dockerfile
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:

bash
docker build -t myimage ./

Quick Start

  • Go to your project directory.
  • Create a Dockerfile with:
dockerfile
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:
python
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route

async def homepage(request):
    return JSONResponse({"message": "Hello World!"})

app = Starlette(routes=[Route("/", homepage)])
  • You should now have a directory structure like:
.
├── app
│   └── main.py
└── Dockerfile
  • Go to the project directory (in where your Dockerfile is, containing your app directory).
  • Build your Starlette image:
bash
docker build -t myimage .
  • Run a container based on your image:
bash
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:

json
{"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:

dockerfile
FROM python:3.11 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.
  • 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 impo

Issues· 1 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Pythonasgiasyncdebiandocker

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类DevOps
定价开源

> 相关工具

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理