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.
Videos
Hi, I am building my first app by myself. I'm using FastAPI, it will be a paid app.
How do I decide whether I should deploy it using docker or just deploy it directly?
Is Docker relatively easy to setup so it makes sense to just use it anyway?