version: "3.9"

services:
  db:
    image: postgres:16.3-alpine
    environment:
      - POSTGRES_DB=django
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=2309
    volumes:
      - pg_data:/var/lib/postgresql/data
    networks:
      - eggs
    deploy:
      replicas: 1
      placement:
        constraints:
          - node.role == manager
      restart_policy:
        condition: any
        max_attempts: 0
      update_config:
        parallelism: 1
        order: start-first
        failure_action: rollback 
        monitor: 30s
        delay: 30s
        max_failure_ratio: 0.2
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

  redis:
    image: redis:alpine 
    command: ["redis-server", "--appendonly", "yes"]
    networks:
      - eggs
    deploy:
      replicas: 1
      placement:
        constraints:
          - node.role == manager
      restart_policy:
        condition: any
      update_config:
        failure_action: rollback 
        monitor: 30s
        delay: 30s
        max_failure_ratio: 0.2
        parallelism: 1
        order: start-first
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 15s
      timeout: 10s
      retries: 5
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

  web:
    image: jscorptech/eggs:22
    env_file:
      - .env
    environment:
      - PYTHONPYCACHEPREFIX=/var/cache/pycache
    networks:
      - eggs
    volumes:
      - /var/log/eggs/:/code/resources/logs/
      - media:/code/resources/media/
      - static:/code/resources/staticfiles/
      - pycache:/var/cache/pycache
    tmpfs:
      - /tmp
      - /run
    deploy:
      replicas: 1
      restart_policy:
        condition: any 
      update_config:
        failure_action: rollback 
        monitor: 30s
        delay: 30s
        max_failure_ratio: 0.2
        parallelism: 1
        order: start-first
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost:8000/health/ || exit 1"]
      interval: 15s
      timeout: 15s
      retries: 5
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

  nginx:
    image: jscorptech/nginx:v1
    ports:
      - mode: ingress 
        target: 80
        published: 8085
        protocol: tcp
    volumes:
      - media:/usr/share/nginx/html/resources/media/:ro
      - static:/usr/share/nginx/html/resources/staticfiles/:ro
    networks:
      - eggs
    deploy:
      replicas: 1
      restart_policy:
        condition: any
      placement:
        constraints:
          - node.role == manager
      update_config:
        failure_action: rollback 
        monitor: 10s
        delay: 10s
        max_failure_ratio: 0.2
        parallelism: 1
        order: start-first
    tmpfs:
      - /var/cache/nginx
      - /var/run
      - /var/tmp
    healthcheck:
      test: ["CMD-SHELL", "curl -f http://localhost/health/ || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5
    logging:
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

networks:
  eggs:
    driver: overlay 

volumes:
  media:
  pg_data:
  pycache:
  static:
