When to use this stack
This stack runs a Django web application behind Gunicorn, with PostgreSQL 16 as the database and Celery for background work such as sending emails, resizing images or calling slow external APIs. Redis 7 acts as the message broker that connects the web process and the worker. Splitting the web process from the worker keeps long tasks away from HTTP requests, and it lets you scale the worker separately when the queue grows.
Choose it when your Django project already uses Celery tasks, or when you know that some requests will take more than a few seconds and should run in the background. It also fits teams that want the same database, broker and environment on every machine. Do not choose it for a small site with no background jobs, where a single Django container is simpler. It is also not a substitute for a managed message queue when you need guaranteed delivery across several regions; Redis here is a single node, and tasks in flight can be lost if it fails without persistence.
The Dockerfile uses the official python:3.12-slim image, installs the packages from requirements.txt and starts Gunicorn on port 8000. Your requirements.txt must include django, gunicorn, celery, redis and psycopg with the binary extra. The settings module must read the database, Redis and secret values from environment variables, because this compose file sets them and never writes them into the code. The module name config is an example; change it in the compose command and the Dockerfile if your project uses another name.
docker-compose.yml
services:
web:
build: ./app
restart: unless-stopped
depends_on:
db:
condition: service_healthy
redis:
condition: service_healthy
ports:
- "8000:8000"
environment:
DJANGO_SECRET_KEY: ${DJANGO_SECRET_KEY}
DJANGO_DEBUG: "0"
DJANGO_ALLOWED_HOSTS: ${DJANGO_ALLOWED_HOSTS}
POSTGRES_HOST: db
POSTGRES_PORT: 5432
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
CELERY_BROKER_URL: redis://:${REDIS_PASSWORD}@redis:6379/0
worker:
build: ./app
restart: unless-stopped
command: ["celery", "-A", "config", "worker", "--loglevel=info"]
depends_on:
db:
condition: service_healthy
redis:
condition: service_healthy
environment:
DJANGO_SECRET_KEY: ${DJANGO_SECRET_KEY}
DJANGO_DEBUG: "0"
DJANGO_ALLOWED_HOSTS: ${DJANGO_ALLOWED_HOSTS}
POSTGRES_HOST: db
POSTGRES_PORT: 5432
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
CELERY_BROKER_URL: redis://:${REDIS_PASSWORD}@redis:6379/0
db:
image: postgres:16-alpine
restart: unless-stopped
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
interval: 10s
timeout: 5s
retries: 5
redis:
image: redis:7-alpine
restart: unless-stopped
environment:
REDIS_PASSWORD: ${REDIS_PASSWORD}
command: ["redis-server", "--requirepass", "${REDIS_PASSWORD}", "--appendonly", "yes"]
volumes:
- redis_data:/data
healthcheck:
test: ["CMD-SHELL", "redis-cli -a \"$$REDIS_PASSWORD\" --no-auth-warning ping | grep PONG"]
interval: 10s
timeout: 5s
retries: 5
volumes:
postgres_data:
redis_data:# Django secret key: a long random string, never reuse it between environments
DJANGO_SECRET_KEY=CHANGE_ME_long_random_django_secret
# Comma-separated host names that Django will serve
DJANGO_ALLOWED_HOSTS=localhost,127.0.0.1
# PostgreSQL database and user
POSTGRES_DB=app
POSTGRES_USER=app
POSTGRES_PASSWORD=CHANGE_ME_postgres_password
# Redis password, used by the Celery broker URL
REDIS_PASSWORD=CHANGE_ME_redis_passwordDockerfile (app/Dockerfile)
Place this file in ./app, next to manage.py and requirements.txt. The worker service reuses the same image.
FROM python:3.12-slim
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "3"].env
# Django secret key: a long random string, never reuse it between environments
DJANGO_SECRET_KEY=CHANGE_ME_long_random_django_secret
# Comma-separated host names that Django will serve
DJANGO_ALLOWED_HOSTS=localhost,127.0.0.1
# PostgreSQL database and user
POSTGRES_DB=app
POSTGRES_USER=app
POSTGRES_PASSWORD=CHANGE_ME_postgres_password
# Redis password, used by the Celery broker URL
REDIS_PASSWORD=CHANGE_ME_redis_passwordStart and verify
docker compose config --quiet
# Build the image and start all four services
docker compose up -d --build
# db and redis must be healthy; web and worker must be running
docker compose ps
# Apply database migrations and collect static files
docker compose exec web python manage.py migrate --noinput
docker compose exec web python manage.py collectstatic --noinput
# The worker log should show "celery@... ready" and the Redis broker
docker compose logs --tail=50 worker
# Confirm that Django answers (expect 200, 301 or 400 for a disallowed host)
curl -I http://localhost:8000Production checklist
- Keep DJANGO_DEBUG at 0 and list only real host names in DJANGO_ALLOWED_HOSTS.
- Put Gunicorn behind a reverse proxy that terminates HTTPS, and set SECURE_PROXY_SSL_HEADER, SESSION_COOKIE_SECURE and CSRF_COOKIE_SECURE in settings.
- Serve static files with WhiteNoise or the proxy. Gunicorn does not serve them.
- Use URL-safe passwords for POSTGRES_PASSWORD and REDIS_PASSWORD, because they are part of the broker URL.
- Keep Redis with persistence enabled and internal to Docker, and never publish port 6379 to the internet.
- Run migrations as a deliberate step at deploy time, not automatically on every start.
- Back up postgres_data with pg_dump and test a restore into a new container.
- Set Celery task time limits and make tasks idempotent, because Redis delivery is at least once in practice after failures.
Common problems
- Symptom: web returns “DisallowedHost” in the logs. Cause: The requested host is not in DJANGO_ALLOWED_HOSTS. Fix: Add the host name, separated by commas, and restart web.
- Symptom: Tasks are queued but never run. Cause: The worker uses a different broker URL, or the worker cannot import the task module. Fix: Compare CELERY_BROKER_URL in both services and read docker compose logs worker for ImportError messages.
- Symptom: “password authentication failed for user” from Django. Cause: The POSTGRES_* values changed after the postgres_data volume was created. Fix: Change the password inside PostgreSQL with ALTER USER, or remove the volume with docker compose down -v on a test install only.
- Symptom: The worker restarts repeatedly with a Redis connection error. Cause: The password in the broker URL has special characters that are not URL-encoded. Fix: Use letters and digits only, or encode the password.
- Symptom: Static files return 404 with DEBUG off. Cause: collectstatic was never run, or STATIC_ROOT is not served. Fix: Run collectstatic in the web container and add WhiteNoise or proxy rules for /static/.
Variations
- Celery beat for schedules: Add a beat service with the same image and command celery -A config beat –loglevel=info. Run exactly one beat container, and set the schedule in settings.
- Scale workers: Run docker compose up -d –scale worker=3 to add consumers. Check that tasks are safe to run in parallel before you do.
- Managed Redis or RabbitMQ: Keep the same worker and web services, and replace the redis service with rabbitmq:3.13-management-alpine for stronger delivery guarantees. Change the broker URL to amqp://, and test failover.
Questions
Which folder must contain my Django project?
The web and worker services build from ./app, so manage.py, requirements.txt and the config package must be in that folder. The code is copied into the image, not bind-mounted.
Why does my Celery worker not pick up tasks?
Check that CELERY_BROKER_URL matches the Redis password, that the worker runs with -A config, and that the task module is imported by the Django project. The worker logs show the broker connection on startup.
Do I need a separate Celery beat container?
Only if you schedule periodic tasks. Add a service that runs celery -A config beat, and run exactly one beat instance, or scheduled tasks run twice.
Adapt this stack
Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.
Open in builderRelated stacks
Rails + PostgreSQL + Redis
Run a Ruby on Rails app with PostgreSQL 16 and Redis 7 in Docker Compose, with a slim Ruby 3.3 Dockerfile, database setup, persistent data and a checklist.
Laravel + PostgreSQL + Redis
Run a Laravel app on Apache with PostgreSQL 16 and Redis 7 in Docker Compose, with a Dockerfile, queue and cache settings, volumes and a production checklist.
Next.js + PostgreSQL + Redis
Containerize a Next.js app with PostgreSQL 16 and Redis 7 in Docker Compose, using a minimal Dockerfile, healthchecks, named volumes and a production checklist.
Directus + PostgreSQL
Run Directus 11 headless CMS on PostgreSQL 16 in Docker Compose, with required keys, admin bootstrap, persistent uploads, a health check and a checklist.
Browse more Web & Frameworks stacks or the full library.