Skip to content
🎉 Launch sale:50% off everything over $22 — automatically applied at checkout· ends Aug 2Shop the sale →
DevOps AI ToolKit
Newsletter

Django + PostgreSQL Docker Compose Example

A Django app with a PostgreSQL backend, health-gated startup, and a private backend network.

The compose.yaml

name: django-app
services:
  web:
    build:
      context: .
    image: django-app:latest
    command: gunicorn config.wsgi:application --bind 0.0.0.0:8000
    restart: unless-stopped
    ports:
      - 8000:8000
    environment:
      DATABASE_URL: postgres://app:${POSTGRES_PASSWORD}@db:5432/app
    networks:
      - frontend
      - backend
    depends_on:
      db:
        condition: service_healthy
    healthcheck:
      test:
        - CMD-SHELL
        - curl -f http://localhost:8000/health/ || exit 1
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s
    logging:
      driver: json-file
      options:
        max-size: 10m
        max-file: '3'
  db:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: app
      POSTGRES_USER: app
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
      - backend
    healthcheck:
      test:
        - CMD-SHELL
        - pg_isready -U app
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s
networks:
  frontend: {}
  backend:
    internal: true
volumes:
  pgdata: {}

Start it up

# Validate the configuration (does not start anything):
docker compose -f compose.yaml config

# Pull images and start in the background:
docker compose -f compose.yaml pull
docker compose -f compose.yaml up -d

# Check status and follow logs:
docker compose -f compose.yaml ps
docker compose -f compose.yaml logs -f

# Stop and remove (named volumes are kept):
docker compose -f compose.yaml down

Services

  • webdjango-app:latest
  • dbpostgres:16-alpine

Security notes

  • warning
    Hard-coded secret in environment
    Reference secrets via `${VAR}` from an untracked `.env`, or use Docker/Compose `secrets:` backed by an external store.
  • warning
    No memory limit
    Set a memory limit sized to the workload (e.g. `mem_limit: 512m`).
  • warning
    No memory limit
    Set a memory limit sized to the workload (e.g. `mem_limit: 512m`).
  • warning
    Service image uses `latest`
    Pin an explicit version tag (and ideally a digest) for every service image.

Open the template in the generator to apply one-click hardening.

FAQ

How do I run this Django + PostgreSQL Docker Compose file?

Save it as compose.yaml, then run `docker compose up -d`. Validate first with `docker compose config` and follow logs with `docker compose logs -f`.

Do I need a version field in the Django + PostgreSQL Compose file?

No — modern Docker Compose does not require a top-level version field, and this example does not include one.

How do I customize this Django + PostgreSQL template?

Open it in the free Docker Compose Generator to change images, ports, volumes, networks, environment variables, and health checks visually, then download your compose.yaml.

Independent educational example. "Docker" is a trademark of Docker, Inc. Review and adapt before using in production.