docker compose for django

Django August 14, 2025 python

docker compose for django

python
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
version: '3.9'

services:
  web_shop:
    build: .
    command: >
      sh -c "
      python manage.py makemigrations  &&
      python manage.py migrate --noinput &&
      python manage.py collectstatic --noinput &&
      daphne -b 0.0.0.0 -p 8000 backend.asgi:application
      "
    healthcheck:
      test: ["CMD-SHELL", "curl -fs http://localhost:8000/ || exit 1"]
      interval: 10s
      timeout: 3s
      retries: 5
      start_period: 20s
    expose:
      - "8000" 
    container_name: django_app_shop
    volumes:
      - ./src:/app
      - static_volume:/app/staticfiles
      - media_volume:/app/media
    depends_on:
      pgbouncer_shop:
        condition: service_healthy
      rabbitmq_shop:
        condition: service_healthy
      redis_cache_shop:
        condition: service_healthy
    environment:
      VARNISH_HOST: varnish_shop
      VARNISH_PORT: 80
    env_file:
      - .env

  varnish_shop:
    image: varnish:7-alpine
    container_name: varnish_shop
    ports:
      - "80:80"
    environment:
      VARNISH_SIZE: 256M
    depends_on:
      nginx_shop:
        condition: service_started
    volumes:
      - ./varnish/default.vcl:/etc/varnish/default.vcl:ro

  celery_worker_shop:
    build: .
    container_name: celery_worker_shop
    command: celery -A backend worker -l info --concurrency=4
    environment:
      CELERY_BROKER_URL: amqp://${RABBITMQ_DEFAULT_USER}:${RABBITMQ_DEFAULT_PASS}@rabbitmq_shop:5672//
      CHANNEL_RABBITMQ:  amqp://${RABBITMQ_DEFAULT_USER}:${RABBITMQ_DEFAULT_PASS}@rabbitmq_shop:5672/
    volumes:
      - ./src:/app
    env_file: .env
    depends_on:
      web_shop:
        condition: service_healthy
      rabbitmq_shop:
        condition: service_healthy

  celery_beat_shop:
    build: .
    container_name: celery_beat_shop
    command: celery -A backend beat -l info
    environment:
      CELERY_BROKER_URL: amqp://${RABBITMQ_DEFAULT_USER}:${RABBITMQ_DEFAULT_PASS}@rabbitmq_shop:5672//
      CHANNEL_RABBITMQ:  amqp://${RABBITMQ_DEFAULT_USER}:${RABBITMQ_DEFAULT_PASS}@rabbitmq_shop:5672/
    volumes:
      - ./src:/app
    env_file: .env
    depends_on:
      web_shop:
        condition: service_healthy
      rabbitmq_shop:
        condition: service_healthy

  redis_cache_shop:
    image: redis:7
    container_name: redis_cache_shop
    command:
      - redis-server
      - --maxmemory
      - 256mb
      - --maxmemory-policy
      - allkeys-lru
    expose:
      - "6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      retries: 5

  rabbitmq_shop:
    image: rabbitmq:3-management
    container_name: rabbitmq_shop
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
      RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER}
      RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS}
    healthcheck:
      test: ["CMD", "rabbitmqctl", "status"]
      interval: 10s
      timeout: 5s
      retries: 5
    env_file:
      - .env

  nginx_shop:
    image: nginx:alpine
    container_name: nginx_shop
    expose:
      - "80"
    volumes:
      - ./config/nginx/django.conf:/etc/nginx/conf.d/default.conf:ro
      - static_volume:/staticfiles:ro
      - media_volume:/media:ro
    depends_on:
      web_shop:
        condition: service_healthy

  db_shop:
    image: postgres:17
    container_name: postgres_db_shop
    volumes:
      - postgres_db_shop:/var/lib/postgresql/data/
    env_file:
      - .env
    environment:
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_DB: ${POSTGRES_DB}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 10s
      start_period: 30s
      timeout: 10s
      retries: 5

  pgbouncer_shop:
    image: edoburu/pgbouncer:latest
    container_name: pgbouncer_shop
    restart: unless-stopped
    depends_on:
      db_shop:
        condition: service_healthy
    ports:
      - "6432:6432"
    env_file:
      - .env
    environment:
      DB_HOST: db_shop
      DB_PORT: 5432
      DB_USER: ${POSTGRES_USER}
      DB_PASSWORD: ${POSTGRES_PASSWORD}
      POOL_MODE: transaction
      MAX_CLIENT_CONN: 200
      DEFAULT_POOL_SIZE: 20
      IGNORE_STARTUP_PARAMETERS: "extra_float_digits"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -h localhost -p 6432 -U ${POSTGRES_USER}"]
      interval: 10s
      retries: 5
    volumes:
      - ./pgbouncer:/etc/pgbouncer

volumes:
  postgres_db_shop:
  static_volume:
  media_volume: