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.
🌐
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.
🌐
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 - If you go to http://127.0.0.1:8000/, ... am using FastAPI" So far, so good. However, we haven’t integrated Docker yet. We will be developing with containers (some argue this is not convenient, but it’s ultimately up to you). Also, we will use uv inside a container, which may be debatable, but it’s what I am used to. uv provides some useful information about using uv in Docker here. We’ll start by adding a Dockerfile at the root ...
🌐
GitHub
github.com › astral-sh › uv-docker-example › blob › main › Dockerfile
uv-docker-example/Dockerfile at main · astral-sh/uv-docker-example
# Reset the entrypoint, don't invoke `uv` ENTRYPOINT [] · # Use the non-root user to run our application · USER nonroot · · # Run the FastAPI application by default · # Uses `uv run` to sync dependencies on startup, respecting UV_NO_DEV · # Uses `fastapi dev` to enable hot-reloading when the `watch` sync occurs ·
Author   astral-sh
🌐
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.
🌐
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%
Find elsewhere
🌐
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 - [project] name = "uv-example" version = "0.1.0" description = "My uv Example Project" requires-python = ">=3.13" dependencies = [ "fastapi[standard]==0.115.5" ] Next, lock the dependencies into the uv.lock file that should also appear in the src/ directory: docker compose -f docker/compose.yaml run --rm uv-example uv lock · With pyproject.toml, uv.lock, and main.py ready, update your Dockerfile to include two additional layers:
🌐
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
🌐
Digon
digon.io › en › blog › 2025_07_28_python_docker_images_with_uv
Build Multistage Python Docker Images Using UV
July 27, 2025 - In this post, we walked through the process of building a minimal, reproducible and production-ready Docker image for a FastAPI application using uv and a multistage Dockerfile. We covered how to structure your Python project for efficient builds, how to manage dependencies with uv sync, and how to isolate the runtime environment for security and performance.
🌐
ARV Personal Website
arv-anshul.github.io › blog › docker-fastapi-uv
Dockerize - FastAPI - UV | ARV
FROM python:3.11-slim AS builder # (1)! COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/ # Install gcc for wordcloud # (2)! RUN apt-get update && apt-get install -y gcc && apt-get clean WORKDIR /app # (3)! ADD pyproject.toml uv.lock /app # Install dependencies with `--extra=backend` dependencies RUN uv sync --extra=backend --frozen --compile-bytecode --no-install-project # (4)! # Copy only necessary files/folders to reduce image size COPY params.yaml /app COPY backend /app/backend COPY ml /app/ml # (5)! RUN uv sync --extra=backend --locked # Final stage # (6)! FROM python:3.11-slim AS final # (7)! COPY --from=builder /app /app WORKDIR /app # Run backend using fastapi-cli # (8)!
🌐
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
Docker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python with performance auto-tuning. - tiangolo/uvicorn-gunicorn-fastapi-docker
Starred by 2.9K users
Forked by 341 users
Languages   Python 81.4% | Dockerfile 18.6%
🌐
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 - FROM python:3.12-alpine # Install system dependencies required by uv RUN apk add --no-cache curl git # Install uv safely RUN curl -LsSf -o /tmp/install_uv.sh https://astral.sh/uv/install.sh && \ sh /tmp/install_uv.sh && \ rm /tmp/install_uv.sh # Set PATH to include uv ENV PATH="/root/.local/bin:$PATH" # Create app directory and set permissions WORKDIR /app # Copy dependency files first for caching COPY pyproject.toml uv.lock ./ # Install dependencies using uv RUN uv sync --frozen --no-install-project --no-dev --python-preference=only-system # Copy the rest of the project files COPY . . # Run the FastAPI application CMD ["uv", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
🌐
Docker Hub
hub.docker.com › r › tiangolo › uvicorn-gunicorn-fastapi
tiangolo/uvicorn-gunicorn-fastapi - Docker Image
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. 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:
🌐
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. Each step is deeply described below. ├── app | |── main.py # FastAPI simple app | ├── .dockerignore # similar to .gitignore | # but for Docker ├── .gitignore | ├── Dockerfile # commands to build | # a Docker Image ├── README.md | ├── requirements.txt # pip freeze from | # pip install fastapi uvicorn
🌐
Medium
medium.com › @benitomartin › deep-dive-into-uv-dockerfiles-by-astral-image-size-performance-best-practices-5790974b9579
Deep Dive into uv Dockerfiles by Astral: Image Size, Performance & Best Practices | by Benito Martin | Medium
March 18, 2025 - # Copy the application from the builder COPY --from=builder --chown=app:app /app /app # Place executables in the environment at the front of the path ENV PATH="/app/.venv/bin:$PATH" # Run the FastAPI application by default CMD ["fastapi", "dev", "--host", "0.0.0.0", "/app/src/uv_docker_example"] ... FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS builder: Begins the first stage (builder) using the uv image with Python 3.12 pre-installed. ENV UV_COMPILE_BYTECODE=1: These environment variables enable bytecode compilation and configure the uv tool to copy files, just as in the previous Dockerfile.
🌐
GitHub
github.com › astral-sh › uv › blob › main › docs › guides › integration › fastapi.md
uv/docs/guides/integration/fastapi.md at main · astral-sh/uv
A guide to using uv with FastAPI to manage Python dependencies, run applications, and deploy with Docker.
Author   astral-sh
🌐
GitHub
github.com › astral-sh › uv-docker-example
GitHub - astral-sh/uv-docker-example: An example of using uv in Docker images · GitHub
The project at pyproject.toml includes Ruff as an example development dependency, includes FastAPI as a dependency, and defines a hello entrypoint for the application. To check that the environment is up-to-date after image builds: $ ./run.sh uv sync --locked Audited 2 packages ...
Starred by 762 users
Forked by 74 users
Languages   Dockerfile 81.4% | Shell 16.2% | Python 2.4%