services:
  mariadb:
    image: mariadb:11
    container_name: gmao-mariadb
    restart: unless-stopped
    env_file:
      - .env.docker
    volumes:
      - gmao_mariadb_data:/var/lib/mysql
      - ./docker/initdb.d:/docker-entrypoint-initdb.d:ro
      - gmao_backups:/backups
    ports:
      - "127.0.0.1:3307:3306"
    healthcheck:
      test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
      interval: 10s
      timeout: 5s
      retries: 5
    deploy:
      resources:
        limits:
          cpus: "1.0"
          memory: 512M
        reservations:
          memory: 128M

  gmao:
    image: gmao-college:${GMAO_VERSION:-0.1.0-dev.11}
    build:
      context: .
      dockerfile: Dockerfile
      args:
        GMAO_VERSION: ${GMAO_VERSION:-0.1.0-dev.11}
        GIT_VERSION: ${GIT_VERSION:-unknown}
    container_name: gmao-flask
    restart: unless-stopped
    depends_on:
      mariadb:
        condition: service_healthy
    ports:
      # Application accessible depuis le reseau; MariaDB et Adminer restent locaux.
      - "0.0.0.0:5080:5080"
    volumes:
      - gmao_uploads:/app/app_new/uploads
      - gmao_instance:/app/app_new/instance
      - gmao_data:/app/data
      - gmao_backups:/backups
    env_file:
      - .env.docker
    environment:
      - TZ=Europe/Paris
      - DB_HOST=mariadb
      - DB_PORT=3306
      - GMAO_VERSION=${GMAO_VERSION:-0.1.0-dev.11}
      - GMAO_GUNICORN_FLAGS=--preload
      - DOCKER_GMAO_WORKDIR=/app
    working_dir: /app
    healthcheck:
      test: ["CMD", "python3", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:5080/health/')"]
      interval: 30s
      timeout: 20s
      retries: 5
      start_period: 60s
    deploy:
      resources:
        limits:
          cpus: '0.9'
          memory: 1G
        reservations:
          memory: 256M
    command: ["/home/gmao/.local/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

  adminer:
    image: adminer
    container_name: gmao-adminer
    restart: unless-stopped
    depends_on:
      mariadb:
        condition: service_healthy
    ports:
      - "0.0.0.0:8080:8080"
    environment:
      - TZ=Europe/Paris
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:8080/"]
      interval: 30s
      timeout: 10s
      retries: 3
    deploy:
      resources:
        limits:
          cpus: '0.2'
          memory: 128M
        reservations:
          memory: 64M

volumes:
  gmao_mariadb_data:
  gmao_backups:
  gmao_uploads:
  gmao_instance:
  gmao_data:
