version: '3.7'

services:
  backend:
    container_name: "${APP_NAME}-backend"
    build:
      context: .
      dockerfile: deployment/Dockerfile
      args:
        - APP_NAME=${APP_NAME}
        - APP_HOST=${APP_HOST}
        - APP_PORT=${APP_PORT}
    volumes:
      - ./src:/usr/src/app/
      - ./deployment/scripts:/app/deployment/scripts/
      - static_files:/usr/src/app/static
      - media_files:/usr/src/app/media
    env_file: .env.prod
    expose:
      - "${APP_PORT:-8000}"
    depends_on:
      - db
    command: [ "/bin/bash", "/app/deployment/scripts/wait-db.sh", "/app/deployment/scripts/backend/start.sh" ]

  db:
    image: postgis/postgis:15-3.4-alpine
    container_name: "${APP_NAME}-db"
    hostname: "${POSTGRES_HOST:-db}"
    volumes:
      - postgres_data_dir:/var/lib/postgresql/data/
    env_file: .env.prod
    expose:
      - "${POSTGRES_PORT:-5432}"
    shm_size: 1g
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "${POSTGRES_USER}", "-d", "${POSTGRES_DB}"]
      interval: 10s
      timeout: 5s
      retries: 5


  redis:
    container_name: "${APP_NAME}-redis"
    image: redis:latest
    command: ["redis-server", "--appendonly", "no", "--maxmemory", "500mb", "--maxmemory-policy", "allkeys-lru"]
    volumes:
        - redis_data:/data

  celery-worker: &celery-worker
    container_name: "${APP_NAME}-celery-worker"
    build:
      context: .
      dockerfile: deployment/Dockerfile
    volumes:
      - ./src:/usr/src/app/
      - ./deployment/scripts:/app/deployment/scripts/
      - static_files:/usr/src/app/static
      - media_files:/usr/src/app/media
    env_file: .env.prod
    depends_on:
      - db
      - redis
      - backend
    command: [ "/bin/bash", "/app/deployment/scripts/wait-db.sh", "/app/deployment/scripts/celery/start-worker.sh" ]

  celery-beat:
    <<: *celery-worker
    container_name: "${APP_NAME}-celery-beat"
    command: [ "/bin/bash", "/app/deployment/scripts/wait-db.sh", "/app/deployment/scripts/celery/start-beat.sh" ]

  nginx:
    container_name: "${APP_NAME}-nginx"
    build:
      context: .
      dockerfile: deployment/Dockerfile-nginx
    volumes:
      - ./deployment/scripts/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - static_files:/usr/src/app/static
      - media_files:/usr/src/app/media
      - ./logs-nginx:/var/log/nginx
    env_file: .env.prod
    ports:
      - 6380:80
    depends_on:
        - backend

volumes:
  static_files:
  media_files:
  postgres_data_dir:
  redis_data:
  log_files:
