Cloud & DevOps
A production checklist for Dockerised FastAPI services
A practical checklist for running FastAPI in Docker in production: images, workers, health checks, configuration, logging, migrations, shutdown and CI.
By Envexa Engineering6 min read
A FastAPI service that works on a laptop is not yet a production service. The gap is made of small, unglamorous details. This checklist covers the ones we see missed most often.
1. Build a small, reproducible image
- Use a slim official base image and pin its version.
- Use a multi-stage build so compilers and caches do not ship in the final image.
- Install from a lock file for reproducible dependencies.
- Run as a non-root user.
- Add a
.dockerignoreso secrets,.gitand local files never enter the build context.
FROM python:3.12-slim AS base
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
WORKDIR /app
FROM base AS deps
COPY requirements.lock .
RUN pip install --no-cache-dir --prefix=/install -r requirements.lock
FROM base AS runtime
COPY --from=deps /install /usr/local
COPY app ./app
RUN useradd --system --uid 10001 appuser
USER appuser
EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]2. Decide how you run workers
A single Uvicorn process uses one CPU core. Either run several worker processes per container, or run one process per container and let your orchestrator scale replicas. Pick one model deliberately. For most container platforms, one process per container with horizontal scaling is simpler to reason about, observe and restart.
3. Health checks that mean something
Separate liveness (the process is running) from readiness (it can serve traffic: database reachable, migrations applied). A readiness endpoint that checks dependencies lets load balancers stop sending requests to an instance that cannot handle them, without constantly restarting it.
4. Configuration from the environment
- Load settings from environment variables, validated at start-up (for example with Pydantic settings). Fail fast on missing values.
- Keep secrets out of the image and out of version control. Inject them at runtime.
- Never log secrets or full request bodies containing personal data.
5. Structured logging and metrics
Log JSON to standard output, with a request ID that is propagated to downstream calls. Track request rate, error rate and latency percentiles. Alert on user-visible symptoms such as elevated 5xx rates and slow p95 latency, not on every internal blip.
6. Database migrations
Run migrations as a separate, explicit step in your deployment, not implicitly when many replicas start at once. Write backwards-compatible migrations (add a column, deploy code that uses it, then remove the old one) so a rollout and a rollback are both safe.
7. Graceful shutdown
When the platform sends SIGTERM, stop accepting new requests, finish in-flight ones and close database connections. Make sure your orchestrator's termination grace period is longer than your slowest normal request. Background workers should finish or safely release the job they hold.
8. CI that gates the release
- Lint and type-check.
- Run tests, including integration tests against a real PostgreSQL container.
- Scan dependencies and the built image for known vulnerabilities.
- Build once, tag the image with the commit SHA and promote that exact image through environments.
- Deploy with a rollback path you have actually tested.
None of this is exotic. Together, it is the difference between a service you hope will stay up and one you can operate with confidence.
Related articles
Background jobs: when a Postgres queue is enough and when to reach for Redis
Database-backed and Redis-backed job queues compared: durability, throughput, transactional enqueueing, retries and idempotency, and how to choose.