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

services:
  mysql:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}mysql:${MYSQL_VERSION:-9.4.0}
    ports:
      - '${MYSQL_PORT_OVERRIDE:-3306}:3306'
    volumes:
      - mysql_data:/var/lib/mysql

      # Initialize database with scripts in ./init.sql
      # - ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    environment:
      TZ: ${TZ:-UTC}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-password}
      MYSQL_ROOT_HOST: ${MYSQL_ROOT_HOST:-%}
    deploy:
      resources:
        limits:
          cpus: ${MYSQL_CPU_LIMIT:-2.0}
          memory: ${MYSQL_MEMORY_LIMIT:-2G}
        reservations:
          cpus: ${MYSQL_CPU_RESERVATION:-0.5}
          memory: ${MYSQL_MEMORY_RESERVATION:-512M}
    healthcheck:
      test: [CMD, mysqladmin, ping, -h, localhost, -u, root, -p$$MYSQL_ROOT_PASSWORD]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

volumes:
  mysql_data:
