In a comment you clarify:

I'm probably looking for a way to dynamically set the nr of workers somewhere without needing to specify this twice

In the Uvicorn Settings documentation, it notes that environment variables with names beginning with UVICORN_ directly translate to Uvicorn settings, and also that these must be proper environment variables and not from a --env-file. That means, without changing anything at all in your code or your image build, you can set the worker count when you run the container.

docker run -d -p 8080:8080 \
  -e UVICORN_WORKERS=8 \
  my-image

If the variable is already set in the host environment, you can use the -e option without a value.

$ cat .envrc
export UVICORN_WORKERS=8
$ uv run fastapi dev
...
$ docker run -d -p 8080:8080 -e UVICORN_WORKERS my-image

You often won't want to run a dev server in a container. The Uvicorn documentation also has a Docker setup example that suggests uv run uvicorn ... as the main container command

CMD ["uv", "run", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

That's similar to the last Dockerfile fragment you show in that it uses uv run, but also similar to the first fragment in that it directly runs the application server without having a main function in your own code.

Answer from David Maze on Stack Overflow
๐ŸŒ
Astral
docs.astral.sh โ€บ uv โ€บ guides โ€บ integration โ€บ fastapi
Using uv with FastAPI | uv
November 17, 2025 - A guide to using uv with FastAPI to manage Python dependencies, run applications, and deploy with Docker.
๐ŸŒ
DEV Community
dev.to โ€บ ismaarce โ€บ scalable-python-backend-building-a-containerized-fastapi-application-with-uv-docker-and-172j
Scalable Python backend: Building a containerized FastAPI Application with uv, Docker, and pre-commit: a step-by-step guide - DEV Community
January 17, 2025 - We can run our project by executing: uv run fastapi dev app/main.py, you should see output similar to the following ยท If you go to http://127.0.0.1:8000/, you will see: message "Hi, I am using FastAPI" So far, so good. However, we havenโ€™t integrated Docker yet.
๐ŸŒ
FastAPI
fastapi.tiangolo.com โ€บ deployment โ€บ docker
FastAPI in Containers - Docker - FastAPI
So, it's important to put this near the end of the Dockerfile, to optimize the container image build times. Set the command to use fastapi run, which uses Uvicorn underneath.
๐ŸŒ
Everyday DevOps
markcallen.com โ€บ getting-started-with-fastapi-using-uv-and-docker-com
FastAPI with uv: Install, Run, and Dockerize FastAPI
January 5, 2026 - Learn how to install FastAPI with uv, run it locally, and Dockerize it for production. A modern, faster alternative to pip and poetry.
๐ŸŒ
Medium
medium.com โ€บ @shaliamekh โ€บ python-package-management-with-uv-for-dockerized-environments-f3d727795044
Python package management with uv for dockerized environments | by Raman Shaliamekh | Medium
December 3, 2024 - With this Dockerfile we install necessary system dependencies, create a non-root user to run the container securely and configure essential environment variables for Python. The uv package manager is copied from a pre-built image, and the application source code is copied into the container. Finally, the container is set to run a FastAPI app using uvicorn.
Find elsewhere
๐ŸŒ
YouTube
youtube.com โ€บ watch
uv - Docker setup with a FastAPI application! | Using uv in containers - YouTube
โ˜•๏ธ ๐—•๐˜‚๐˜† ๐—บ๐—ฒ ๐—ฎ ๐—ฐ๐—ผ๐—ณ๐—ณ๐—ฒ๐—ฒ:To support the channel and encourage new videos, please consider buying me a coffee here:https://ko-fi.com/bugbytesโญTop resour...
Published ย  October 23, 2024
๐ŸŒ
GitHub
github.com โ€บ astral-sh โ€บ uv-fastapi-example
GitHub - astral-sh/uv-fastapi-example ยท GitHub
An example of a FastAPI application managed as a uv project.
Starred by 170 users
Forked by 36 users
Languages ย  Python 88.6% | Dockerfile 11.4%
๐ŸŒ
Digon
digon.io โ€บ en โ€บ blog โ€บ 2025_07_28_python_docker_images_with_uv
Build Multistage Python Docker Images Using UV
July 27, 2025 - COPY README.md src ./ RUN uv sync --no-dev --locked --no-editable ยท In the final layer of the builder stage, we run the generate_openapi_json command to generate the OpenAPI specification file (openapi.json) based on the FastAPI application into the app directory.
๐ŸŒ
Readthedocs
docker-fastapi-projects.readthedocs.io โ€บ en โ€บ latest โ€บ uvicorn.html
Uvicorn โ€” Docker FastAPI projects 0.0.2 documentation
This dead simple application shows you to how to create a simple Docker Image with running FastAPI app with Uvicorn and presenting the basics of creating and running Docker Images.
๐ŸŒ
DEV Community
dev.to โ€บ mo7amed_3bdalla7 โ€บ efficient-python-dependency-management-with-uv-in-docker-for-fastapi-1oe1
Efficient Python Dependency Management with uv in Docker for FastAPI - DEV Community
March 5, 2025 - Since dependencies were already installed earlier, Docker will skip unnecessary installations if the code changes. CMD ["uv", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"] Finally, we use uv to launch the FastAPI application with uvicorn as the ASGI server.
๐ŸŒ
GitHub
github.com โ€บ tiangolo โ€บ uvicorn-gunicorn-fastapi-docker
GitHub - tiangolo/uvicorn-gunicorn-fastapi-docker: Docker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python with performance auto-tuning. ยท GitHub
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. There is a sibling Docker image: tiangolo/uvicorn-gunicorn-starlette
Starred by 2.9K users
Forked by 341 users
Languages ย  Python 81.4% | Dockerfile 18.6%
๐ŸŒ
Pybites
pybit.es โ€บ articles โ€บ fastapi-deployment-made-easy-with-docker-and-fly-io
FastAPI Deployment Made Easy with Docker and Fly.io โ€“ Pybites
March 18, 2025 - The Dockerfile is the key component that tells Docker how to set up the environment for the FastAPI app. # Use the full Python image instead of slim to avoid missing system dependencies FROM python:3.11 # Set environment variables ENV PYTHONUNBUFFERED=1 \ UV_NO_INDEX=1 \ DEBIAN_FRONTEND=noninteractive # Set the working directory inside the container WORKDIR /app # Install uv globally first RUN pip install --no-cache-dir uv # Install dependencies globally (avoiding virtual env issues) COPY pyproject.toml ./ RUN uv pip install --system -r pyproject.toml # Copy the rest of the application code COPY .
๐ŸŒ
GitHub
github.com โ€บ loftwah โ€บ uv-fastapi-ecs
GitHub - loftwah/uv-fastapi-ecs: A demo repo of using UV and FastAPI with Docker on AWS ECS. ยท GitHub
A demo repository showing how to use UV package manager with FastAPI, running behind Nginx in Docker, designed for AWS ECS deployment.
Starred by 5 users
Forked by 2 users
Languages ย  HCL 81.9% | Shell 15.6% | Dockerfile 1.6% | Python 0.9%
๐ŸŒ
DevOps.dev
blog.devops.dev โ€บ a-scalable-approach-to-fastapi-projects-with-postgresql-alembic-pytest-and-docker-using-uv-78ebf6f7fb9a
A Scalable Approach to FastAPI Projects with PostgreSQL, Alembic, Pytest, and Docker Using uv | by Arafat Hussain | DevOps.dev
May 28, 2025 - Visit http://localhost:8081/health to confirm it works. Thanks to uv, the Docker build is efficient: it locks dependencies and creates the venv inside the image for a repeatable build.
๐ŸŒ
Docker Hub
hub.docker.com โ€บ r โ€บ tiangolo โ€บ uvicorn-gunicorn-fastapi
tiangolo/uvicorn-gunicorn-fastapi - Docker Image
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.