version: '3.8'

volumes:
  production_postgres_data: {}
  production_postgres_data_backups: {}
  production_esdata: {}
  production_django_static: {}
  production_django_media: {}

services:
  django:
    build:
      context: .
      dockerfile: ./compose/production/django/Dockerfile
    image: diarios_oficiais_alems_production_django
    container_name: diarios_oficiais_alems_production_django
    restart: unless-stopped
    depends_on:
      - postgres
      - elasticsearch
    volumes:
      - production_django_media:/app/diarios_oficiais_alems/media
      - production_django_static:/app/diarios_oficiais_alems/static
    env_file:
      - ./.envs/.production/.django
      - ./.envs/.production/.postgres
    ports:
      - '8005:8005'
    command: /start

  postgres:
    build:
      context: .
      dockerfile: ./compose/production/postgres/Dockerfile
    image: diarios_oficiais_alems_production_postgres
    container_name: diarios_oficiais_alems_production_postgres
    restart: unless-stopped
    volumes:
      - production_postgres_data:/var/lib/postgresql/data
      - production_postgres_data_backups:/backups
    env_file:
      - ./.envs/.production/.postgres
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U debug"]
      interval: 10s
      timeout: 5s
      retries: 5

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:8.17.3
    container_name: diarios_oficiais_alems_production_elasticsearch
    restart: unless-stopped
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=true
      - "ES_JAVA_OPTS=-Xms2g -Xmx2g"
      - bootstrap.memory_lock=true
    volumes:
      - production_esdata:/usr/share/elasticsearch/data
    ulimits:
      memlock:
        soft: -1
        hard: -1
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:9200/_cluster/health || exit 1"]

  frontend:
    image: nginx:alpine
    container_name: diarios_oficiais_alems_production_frontend
    restart: unless-stopped
    depends_on:
      - django
    volumes:
      - production_django_static:/usr/share/nginx/static:ro
      - production_django_media:/usr/share/nginx/media:ro
      - ./compose/production/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    ports:
      - "80:80"
      - "443:443"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost"]
      interval: 30s
      timeout: 10s
      retries: 3

