services:

  db:
    image: postgres:16-alpine
    restart: always
    volumes:
      - postgres_data_caddy_local:/var/lib/postgresql/data
    env_file: .env.prod
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
      interval: 10s
      timeout: 5s
      retries: 5

  web:
    build: .
    restart: always
    env_file: .env.prod
    environment:
      ALLOWED_HOSTS: localhost,127.0.0.1,web
      SECURE_SSL_REDIRECT: "false"
    volumes:
      - static_volume_caddy_local:/app/staticfiles
      - media_volume_caddy_local:/app/media
    depends_on:
      db:
        condition: service_healthy
    command: sh -c "python manage.py migrate --noinput && gunicorn config.wsgi:application --bind 0.0.0.0:8000 --workers 2 --timeout 60 --access-logfile - --error-logfile -"

  caddy:
    image: caddy:2-alpine
    restart: always
    ports:
      - "8890:80"
    volumes:
      - ./Caddyfile.local:/etc/caddy/Caddyfile:ro
      - static_volume_caddy_local:/app/staticfiles:ro
      - media_volume_caddy_local:/app/media:ro
    depends_on:
      - web

volumes:
  postgres_data_caddy_local:
  static_volume_caddy_local:
  media_volume_caddy_local:
