FastAPI + Docker + PostgreSQL Production Setup in Python 2026

Web Development Mar 16, 2026

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:

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices