x-logging: &default-logging
  driver: json-file
  options:
    max-size: "10m"
    max-file: "5"

services:
  app:
    build:
      context: ..
      dockerfile: src/app/Dockerfile
    container_name: devops-app
    ports:
      - "8000:8000"
    environment:
      - ENV=development
      - HOSTNAME=devops-local
    volumes:
      - ../src/app:/app
      - app-logs:/app/logs
    networks:
      - devops-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    labels:
      - "com.devops.description=Application FastAPI"
      - "prometheus.scrape=true"
      - "prometheus.port=8000"
      - "prometheus.path=/metrics"
    logging: *default-logging

  prometheus:
    image: prom/prometheus:v2.47.2
    container_name: devops-prometheus
    ports:
      - "9090:9090"
    volumes:
      - ../config/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - ../config/alert_rules.yml:/etc/prometheus/alert_rules.yml
      - prometheus-data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--storage.tsdb.retention.time=200h'
      - '--web.enable-lifecycle'
    networks:
      - devops-network
    restart: unless-stopped
    depends_on:
      - app
    logging: *default-logging

  grafana:
    image: grafana/grafana:10.2.0
    container_name: devops-grafana
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin123
      - GF_SECURITY_ADMIN_USER=admin
      - GF_INSTALL_PLUGINS=grafana-piechart-panel,grafana-clock-panel
      - GF_USERS_ALLOW_SIGN_UP=false
      - GF_SERVER_DOMAIN=localhost
      - TZ=Europe/Paris
    volumes:
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
      - grafana-data:/var/lib/grafana
      - ../monitoring/grafana-dashboards:/etc/grafana/provisioning/dashboards
      - ../monitoring/grafana-datasources:/etc/grafana/provisioning/datasources
    networks:
      - devops-network
    restart: unless-stopped
    depends_on:
      - prometheus
    logging: *default-logging

  portainer:
    image: portainer/portainer-ce:latest
    container_name: devops-portainer
    ports:
      - "9000:9000"
      - "9443:9443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - portainer-data:/data
    networks:
      - devops-network
    restart: unless-stopped
    command: -H unix:///var/run/docker.sock
    logging: *default-logging

networks:
  devops-network:
    driver: bridge
    name: devops-network

volumes:
  prometheus-data:
    name: prometheus-data
  grafana-data:
    name: grafana-data
  portainer-data:
    name: portainer-data
  app-logs:
    name: app-logs   