FastAPI + Docker + PostgreSQL Production Setup in Python 2026
Running FastAPI applications in production requires a robust, scalable, and secure infrastructure. In 2026, the combination of FastAPI, Docker, PostgreSQL, and proper orchestration has become the gold standard for professional backend deployments.
TL;DR — Production Stack 2026
- FastAPI + Uvicorn/Gunicorn for the API layer
- PostgreSQL with asyncpg driver
- Redis for caching and rate limiting
- Docker + Docker Compose for local development
- Kubernetes or Docker Swarm for production orchestration
1. Production-Ready Dockerfile
FROM python:3.14-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
FROM python:3.14-slim
WORKDIR /app
RUN useradd -m -u 1000 fastapi
COPY --from=builder /usr/local/lib/python3.14/site-packages /usr/local/lib/python3.14/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin
COPY . .
RUN chown -R fastapi:fastapi /app
USER fastapi
EXPOSE 8000
CMD ["gunicorn", "app.main:app", "--workers", "4", "--worker-class", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:8000"]
2. docker-compose.yml for Local Development
version: '3.9'
services:
api:
build: .
ports:
- "8000:8000"
environment:
- DATABASE_URL=postgresql+asyncpg://postgres:password@db:5432/appdb
- REDIS_URL=redis://redis:6379/0
depends_on:
- db
- redis
restart: unless-stopped
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: appdb
volumes:
- postgres_data:/var/lib/postgresql/data
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
postgres_data:
3. Best Production Practices 2026
- Use multi-stage Docker builds to keep images small
- Run with Gunicorn + Uvicorn workers (4–8 workers per CPU core)
- Use environment variables and Docker Secrets for sensitive data
- Implement health checks and graceful shutdown
- Use PostgreSQL connection pooling
- Separate worker containers from the main API
- Monitor with Prometheus + Grafana
Conclusion
A well-designed FastAPI + Docker + PostgreSQL setup in 2026 provides excellent performance, scalability, and developer experience. By following multi-stage builds, proper worker configuration, and separation of concerns, you can deploy reliable, production-grade APIs with confidence.
Next steps:
- Implement this production-ready Docker setup in your FastAPI projects
- Related articles: FastAPI Project Structure Best Practices 2026 • API Performance Optimization with FastAPI 2026