Deploying Django to Production: Docker, Nginx & Gunicorn

By NSLTD | Published on June 25, 2025

DevOps

From Development to Production: A Scalable Deployment Setup

Deploying Django in production is more than just copying files—it's about creating a robust, secure, and maintainable environment. This guide combines Docker, Gunicorn, and Nginx to achieve that.

Step 1: Create a Dockerfile

FROM python:3.11

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

CMD ["gunicorn", "myproject.wsgi:application", "--bind", "0.0.0.0:8000"]

Step 2: Add a docker-compose.yml

version: '3'

services:
  web:
    build: .
    ports:
      - "8000:8000"
    volumes:
      - .:/app
    depends_on:
      - db

  db:
    image: postgres
    environment:
      POSTGRES_DB: mydb
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass

Step 3: Configure Nginx as a Reverse Proxy

server {
    listen 80;
    server_name mydomain.com;

    location / {
        proxy_pass http://web:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Bonus Tips

  • Use .env files for secrets and configs
  • Set DEBUG = False and configure allowed hosts
  • Add static file handling with whitenoise or Nginx
  • Use a process manager like supervisord if not using Docker

With this setup, your Django app is ready to scale, deploy securely, and handle real-world traffic.

Comments

No comments yet. Be the first to comment!

You must be logged in to leave a comment.