Django Production Deployment: Docker, Gunicorn, Nginx & PostgreSQL

Deploying a web application to production requires containerization, process supervision, SSL termination, and reliable database connection pooling. Docker with Gunicorn, Nginx, and PostgreSQL is the gold standard production architecture.

Django Multi-Container Production Architecture Diagram

Figure 44.1: Enterprise Multi-Container Cloud Architecture — Edge CDN, Nginx Reverse Proxy, Gunicorn, PostgreSQL & Redis.

Step 1: Production Multi-Stage Dockerfile

# Dockerfile
FROM python:3.12-slim-bookworm AS builder

# Prevent Python from writing .pyc files and buffer output
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /app

# Install system build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential libpq-dev && \
    rm -rf /var/lib/apt/lists/*

# Install Python requirements
COPY requirements.txt .
RUN pip install --no-cache-dir --prefix=/install -r requirements.txt

# Final Lean Production Image
FROM python:3.12-slim-bookworm

WORKDIR /app

# Install runtime PostgreSQL client library
RUN apt-get update && apt-get install -y --no-install-recommends \
    libpq5 && rm -rf /var/lib/apt/lists/*

# Copy installed packages from builder stage
COPY --from=builder /install /usr/local

# Create non-root user
RUN addgroup --system django && adduser --system --group django

# Copy project code and assign ownership
COPY . .
RUN chown -R django:django /app

# Switch to non-root user
USER django

EXPOSE 8000

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "4", "mywebsite.wsgi:application"]

Step 2: Orchestration with docker-compose.yml

# docker-compose.yml
version: '3.8'

services:
  web:
    build: .
    restart: always
    env_file: .env.production
    depends_on:
      - db
      - redis
    ports:
      - "8000:8000"

  db:
    image: postgres:16-alpine
    restart: always
    environment:
      POSTGRES_DB: veewom_db
      POSTGRES_USER: veewom_user
      POSTGRES_PASSWORD: secure_db_password_2026
    volumes:
      - postgres_data:/var/lib/postgresql/data

  redis:
    image: redis:7-alpine
    restart: always
    volumes:
      - redis_data:/data

volumes:
  postgres_data:
  redis_data:

One-Command Launch

Launch your entire production stack with automated database initialization in a single command:
docker-compose up -d --build

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What role does Nginx play when deployed in front of Gunicorn?

Nginx acts as a high-performance reverse proxy and SSL terminator. It directly serves static and media files at C-speed, compresses assets with Gzip/Brotli, buffers slow client connections, and enforces rate-limiting before forwarding dynamic requests to Gunicorn.

Q2. How many Gunicorn worker processes should be configured?

The standard recommended formula is: `workers = (2 * CPU_cores) + 1`. For a 4-core virtual server, configure 9 workers to ensure full CPU utilization during I/O wait cycles.

Q3. Why should Django containers run under a non-root user?

Running containers as `root` creates a critical security risk — if an attacker exploits a remote code execution vulnerability in a Python package, they gain root access to the underlying container host. Creating and using a dedicated non-root user (e.g. `django-user`) confines the attack.

Q4. How do you automate database migrations during container spin-up?

Use an entrypoint shell script (`entrypoint.sh`) that waits for PostgreSQL to accept socket connections, runs `python manage.py migrate --noinput`, runs `python manage.py collectstatic --noinput`, and then executes `gunicorn`.

Explore Related Django Tutorials & Next Steps

Broaden your backend architecture, REST API design, and production engineering expertise:

Settings.py & Environment Variables

Pass production environment secrets safely into Docker containers.

Static Files Configuration & WhiteNoise

Configure Nginx to serve static files with aggressive caching.

Django Security Hardening

Complete pre-flight deployment audits and SSL certificate provisioning.

Django Career & Freelancing Roadmap

Highlight DevOps and cloud deployment expertise on your developer resume.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!