x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

services:
  grafana:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}grafana/grafana:${GRAFANA_VERSION:-12.3.2}
    ports:
      - '${GRAFANA_PORT_OVERRIDE:-3000}:3000'
    volumes:
      - grafana_data:/var/lib/grafana
      - grafana_logs:/var/log/grafana

      # Optional: Mount custom configuration
      # - ./grafana.ini:/etc/grafana/grafana.ini
      # - ./provisioning:/etc/grafana/provisioning
    environment:
      - TZ=${TZ:-UTC}
      - GF_SECURITY_ADMIN_USER=${GRAFANA_ADMIN_USER:-admin}
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_ADMIN_PASSWORD:-admin}
      - GF_USERS_ALLOW_SIGN_UP=${GRAFANA_ALLOW_SIGN_UP:-false}
      - GF_INSTALL_PLUGINS=${GRAFANA_PLUGINS:-}
      - GF_SERVER_ROOT_URL=${GRAFANA_ROOT_URL:-http://localhost:3000}
      - GF_SECURITY_SECRET_KEY=${GRAFANA_SECRET_KEY:-}
    user: '472:472' # Grafana user
    deploy:
      resources:
        limits:
          cpus: ${GRAFANA_CPU_LIMIT:-1.0}
          memory: ${GRAFANA_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${GRAFANA_CPU_RESERVATION:-0.1}
          memory: ${GRAFANA_MEMORY_RESERVATION:-256M}
    healthcheck:
      test:
        - CMD
        - wget
        - --no-verbose
        - --tries=1
        - --spider
        - 'http://localhost:3000/api/health'

      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

volumes:
  grafana_data:
  grafana_logs:
