Docker + Django + Redis + Celery Complete Setup Guide

Docker + Django + Redis + Celery Complete Setup Guide

This is a real production-level architecture.

Architecture

  • Django → Web App

  • PostgreSQL → Database

  • Redis → Broker

  • Celery → Background tasks

  • Nginx → Reverse proxy

Key Technologies

  • Django

  • Redis

  • Celery

  • PostgreSQL

docker-compose.yml

version: "3.9"

services:
  web:
    build: .
    command: gunicorn project.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
      - redis

  db:
    image: postgres:15
    environment:
      POSTGRES_DB: mydb
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword

  redis:
    image: redis:7

  celery:
    build: .
    command: celery -A project worker -l info
    depends_on:
      - redis
      - db

Django Celery Config

# project/celery.py

from celery import Celery
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")

app = Celery("project")
app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks()

settings.py

CELERY_BROKER_URL = "redis://redis:6379/0"

Now you can run:

docker-compose up --build


Docker Production Deployment on AWS

When deploying to cloud, common options:

  • Amazon ECS

  • Amazon EKS

  • AWS Elastic Beanstalk

  • Amazon EC2

Simple Production Setup (EC2 + Docker)

Step 1: Launch EC2

  • Ubuntu instance

  • Open ports 80 & 443

Step 2: Install Docker

sudo apt update
sudo apt install docker.io

Step 3: Pull Image

docker pull yourdockerhub/django-app

Step 4: Run with Nginx Reverse Proxy

Use:

  • Gunicorn inside container

  • Nginx on host or container

  • HTTPS via Certbot

Production Best Practices

  • Use environment variables

  • Use secrets manager

  • Enable logging

  • Use health checks

  • Auto-scaling group

  • Use RDS instead of container DB

Dockerfile Optimization Deep Dive

Bad Dockerfile

FROM python:3.12
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

Problems:

  • Large image

  • No caching

  • Copies everything

Optimized Version

FROM python:3.12-slim

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

USER nobody

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

Multi-Stage Example

FROM python:3.12 as builder

WORKDIR /install
COPY requirements.txt .
RUN pip install --prefix=/install -r requirements.txt

FROM python:3.12-slim
WORKDIR /app
COPY --from=builder /install /usr/local
COPY . .
CMD ["python", "app.py"]

Key Optimization Tips

  • Order layers properly

  • Use slim images

  • Use specific versions

  • Clean apt cache

  • Reduce build context

  • Avoid root user

  • Use healthcheck

Example:

HEALTHCHECK CMD curl --fail http://localhost:8000 || exit 1

Comments

Popular posts from this blog

Database Integration in FastAPI (SQLAlchemy CRUD)

Middleware & CORS in FastAPI

Python Data Handling