services:
  postgres-server:
    image: postgres:16-alpine
    restart: unless-stopped
    volumes:
      - ${POSTGRES_DATA_DIR}:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    networks:
      - database
    stop_grace_period: 1m

  zabbix-server:
    image: zabbix/zabbix-server-pgsql:ubuntu-7.4-latest
    restart: unless-stopped
    ports:
      - "10051:10051"
    extra_hosts:
      - "haos.alogins.net:192.168.1.3"
    environment:
      DB_SERVER_HOST: postgres-server
      DB_SERVER_PORT: 5432
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - /etc/localtime:/etc/localtime:ro
    ulimits:
      nproc: 65535
      nofile:
        soft: 20000
        hard: 40000
    depends_on:
      - postgres-server
    networks:
      - database
      - backend
      - frontend
    stop_grace_period: 30s

  zabbix-web:
    image: zabbix/zabbix-web-apache-pgsql:ubuntu-7.4-latest
    restart: unless-stopped
    ports:
      - "${WEB_PORT}:8080"
    environment:
      ZBX_SERVER_HOST: zabbix-server
      ZBX_SERVER_PORT: 10051
      DB_SERVER_HOST: postgres-server
      DB_SERVER_PORT: 5432
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
      PHP_TZ: ${PHP_TZ}
    volumes:
      - /etc/localtime:/etc/localtime:ro
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/ping"]
      interval: 1m30s
      timeout: 3s
      retries: 3
      start_period: 40s
      start_interval: 5s
    depends_on:
      - postgres-server
      - zabbix-server
    networks:
      - database
      - backend
      - frontend
    stop_grace_period: 10s

  zabbix-agent:
    image: zabbix/zabbix-agent:ubuntu-7.4-latest
    restart: unless-stopped
    environment:
      ZBX_HOSTNAME: ${AGENT_HOSTNAME}
      ZBX_SERVER_HOST: zabbix-server
      ZBX_SERVER_ACTIVE: zabbix-server
    volumes:
      - /etc/localtime:/etc/localtime:ro
    privileged: true
    pid: host
    depends_on:
      - zabbix-server
    networks:
      - backend
    stop_grace_period: 5s

networks:
  frontend:
    driver: bridge
  backend:
    driver: bridge
    internal: true
  database:
    driver: bridge
    internal: true
