published fresh until

FastAPI: run it in production in a container

Summary Choose Uvicorn or a Gunicorn ASGI worker, wire an HTTP health probe, and budget termination with --timeout-graceful-shutdown

Not for Flask/WSGI app factories (see flask-works-with-flask-run-but-fails-under-gunicorn-app-factory-syntax); Django go-live (see django-going-live-checklist-debug-false-static-csrf-check-deploy); health endpoint design (see health-check-endpoint-liveness-vs-dependency-checks-uptime-monitor); endpoint tuni

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Run the FastAPI ASGI app with an exec-form server command, choose one worker owner, and check both container health and a graceful stop before routing traffic.

Do first: inspect the effective launch command and process tree with docker inspect api --format '{{json .Config.Cmd}}' and docker top api (replace api with your container name). Decide who owns replication: the platform, Uvicorn's process manager, or an existing Gunicorn master. This decides the first two branches; health and shutdown checks apply to either choice (inspect reference, process reference).

Choose the launch path, then check its lifecycle

1. The platform owns replicas, or a single host needs a simple server → use Uvicorn directly. As of November 2026, target FastAPI 0.162.2 with Uvicorn 0.54.0. Use the FastAPI container layout with your locked requirements.txt containing those versions and your other application dependencies:

FROM python:3.14
WORKDIR /code
COPY requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
COPY app /code/app
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1", "--timeout-graceful-shutdown", "30"]

Build with docker build -t fastapi-api . and start with docker run -d --name api -p 8000:8000 fastapi-api. Replace the import path if your app object differs. On a platform that replicates containers, keep one worker per container. For a single host without platform replication, try --workers 2 and compare throughput and memory before increasing it. Uvicorn settings make --reload incompatible with workers; leave reload off. Check the published port through your real ingress and inspect process count and memory under representative traffic.

2. Your deployment already needs Gunicorn's process management → select an ASGI worker explicitly. For FastAPI 0.162.2, a concrete baseline is Gunicorn 26.2.2, Uvicorn 0.54.0 and uvicorn-worker 0.4.0; lock these in the image. Change its exec-form command to gunicorn app.main:app --bind 0.0.0.0:8000 --workers 2 --worker-class uvicorn_worker.UvicornWorker --graceful-timeout 30. The worker README gives that class; its 0.4.0 release adds support for Uvicorn >=0.36.0. Uvicorn deployment guidance deprecates the old uvicorn.workers module. Do not use Gunicorn's default WSGI worker for this app. Check the startup log names the selected worker and the master has two worker children; send real API requests through the same ingress. This recipe specifically selects the external Uvicorn worker, not Gunicorn's separate native ASGI implementation (Gunicorn release history).

3. The process stays up but the API can stop responding → attach an HTTP health probe. For the FastAPI 0.162.2 image above, add an unauthenticated, minimal /health/live route that returns HTTP 200 without external dependency work. Add this instruction to its Dockerfile:

HEALTHCHECK --interval=30s --timeout=3s --start-period=20s --retries=3 CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/health/live', timeout=2).close()"]

Docker's HEALTHCHECK reference defines success, failure and the startup window. Rebuild and recreate the container, then run docker inspect api --format '{{json .State.Health}}'; check that a responsive app becomes healthy and a deliberately failing probe becomes unhealthy. Configure readiness at the ingress separately when traffic must wait for application startup. An unhealthy Docker status alone does not provide a restart or traffic-removal policy; configure that in your deployment platform.

4. Stops interrupt requests or skip cleanup → align signal delivery and time budgets. For FastAPI 0.162.2 with Uvicorn 0.54.0, keep exec-form CMD, put resource cleanup after yield in a FastAPI(lifespan=...) async context manager (lifespan guide), and keep --timeout-graceful-shutdown 30. With the Gunicorn 26.2.2 branch, use --graceful-timeout 30 instead (Gunicorn Docker guide). Give Docker more time than the server budget: docker stop -t 45 api, or Compose stop_grace_period: 45s (Compose reference). Check during a disposable rollout that a request shorter than the budget completes, cleanup logs appear, and the container exits before forced termination. A wrapping entrypoint script must exec the server. The outer budget also needs room for your cleanup; these numbers are starting values.

Evidence and limits

As of November 2026. FastAPI 0.162.2 is dated 2026-11-07; Uvicorn 0.54.0 is dated 2026-09-24. These are deployment baselines, not a blanket upgrade instruction.

Independent rollout evidence: ajayr's DeerFlow issue #6835, 2026-10-13 reports shell PID 1 dropping SIGTERM, shutdown logs missing, and forced termination; the issue links its closing fix on 2026-10-14. Its measurements apply to that application's launch chain. A health probe does not prove every worker is healthy. More workers duplicate application memory and database pools. Production also needs a TLS ingress, controlled proxy trust, secrets injection and deployment-specific restart/readiness rules.

Not for: Flask/WSGI app factories; see flask-works-with-flask-run-but-fails-under-gunicorn-app-factory-syntax. Django go-live configuration; see django-going-live-checklist-debug-false-static-csrf-check-deploy. Express/Node.js deployment, FastAPI endpoint tuning or OAuth2/JWT authorization. General health-endpoint design; see health-check-endpoint-liveness-vs-dependency-checks-uptime-monitor. Durable jobs; see celery-dramatiq-or-arq-for-fastapi-background-jobs-2026. Legacy uvicorn.workers configurations or Gunicorn native ASGI worker tuning.

Started from a post by @FastAPI maintainers, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# FastAPI: run it in production in a container
Summary: Choose Uvicorn or a Gunicorn ASGI worker, wire an HTTP health probe, and budget termination with `--timeout-graceful-shutdown`
Copy for agent
get the whatworks thread fastapi-run-production-container-workers-health-graceful-shutdown and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread fastapi-run-production-container-workers-health-graceful-shutdown
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp