services:

  drill-ui:
    image: ghcr.io/drill4j/drill4j-ui:${DRILL_UI_VERSION}
    environment:
      API_HOST: host.docker.internal
      API_PORT: 8090
    extra_hosts:
      - "host.docker.internal:host-gateway"
    ports:
      - "8091:8080"

  drill-admin:
    image: ghcr.io/drill4j/admin:${DRILL_ADMIN_BACKEND_VERSION}
    environment:
      - DRILL_METRICS_UI_BASE_URL=${DRILL_METRICS_UI_BASE_URL}
      - DRILL_DB_PORT=5432
      - DRILL_AGENTS_SOCKET_TIMEOUT=6000
      - DRILL_DB_HOST=${POSTGRES_HOST}
      - DRILL_DB_NAME=${POSTGRES_DB}
      - DRILL_DB_USER_NAME=${POSTGRES_USER}
      - DRILL_DB_PASSWORD=${POSTGRES_PASSWORD}
      - DRILL_DB_MAX_POOL_SIZE=${DRILL_ADMIN_BACKEND_DB_MAX_POOL_SIZE}
      - LOG_LEVEL=info
    healthcheck:
      test: [ "CMD", "curl", "http://localhost:8090" ]
      interval: 10s
      timeout: 3s
      retries: 30
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "5"
    ports:
      - 8090:8090
    depends_on:
      postgres:
        condition: service_healthy

  postgres:
    image: postgres:17.0
    shm_size: 1g
    ports:
      - '5432:5432'
    environment:
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      # Volume to initialize Metabase dashboards 
      # Place data.sql file from the https://github.com/Drill4J/drill-metabase-dashboards/releases to db-init directory
      - ./db-init:/docker-entrypoint-initdb.d
      # Volume to store application metrics
      - drill-data-pg:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U postgres']
      interval: 10s
      timeout: 5s
      retries: 5
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "5"
    command: ["postgres", "-c", "log_statement=all", "-c", "log_min_duration_statement=0"]

  # This container launches script to either create or update Metabase dashboards
  # WARNING: it removes all previously created Metabase dashboards and users
  # comment this out if you'd like to keep your edits / customizations
  drill-metabase-dashboards-migration:
    image: ghcr.io/drill4j/drill-metabase-dashboards:${DRILL_METABASE_DASHBOARDS_VERSION}
    environment:      
      POSTGRES_HOST: ${POSTGRES_HOST}
      POSTGRES_PORT: ${POSTGRES_PORT}
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_SSL: ${POSTGRES_SSL}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      METABASE_DB_NAME: ${METABASE_DB_NAME}
      DRILL_METRICS_UI_BASE_URL: ${DRILL_METRICS_UI_BASE_URL}
      DRILL_UI_BASE_URL: ${DRILL_UI_BASE_URL}
    extra_hosts:
      - "host.docker.internal:host-gateway"
    depends_on:
          postgres:
            condition: service_healthy

  metabase:
    image: metabase/metabase:v0.53.7.x
    container_name: metabase
    hostname: metabase
    volumes:
      # Required to share urandom from host machine to speedup startup (see https://github.com/metabase/metabase/issues/10175, https://ruleoftech.com/2016/avoiding-jvm-delays-caused-by-random-number-generation)
      - /dev/urandom:/dev/random:ro
    ports:
      - 8095:3000
    environment:
      MB_DB_TYPE: postgres
      MB_DB_HOST: ${POSTGRES_HOST}
      MB_DB_PORT: ${POSTGRES_PORT}
      MB_DB_USER: ${POSTGRES_USER}
      MB_DB_PASS: ${POSTGRES_PASSWORD}
      MB_DB_DBNAME: ${METABASE_DB_NAME}
    healthcheck:
      test: curl --fail -I http://localhost:3000/api/health || exit 1
      interval: 15s
      timeout: 5s
      retries: 5
    depends_on:
      drill-metabase-dashboards-migration:    
        condition: service_completed_successfully

volumes:
  drill-data-pg:
