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

services:
  kodbox:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}kodcloud/kodbox:${KODBOX_VERSION:-1.62}
    ports:
      - '${KODBOX_PORT:-80}:80'
    volumes:
      - kodbox_data:/var/www/html
    environment:
      - TZ=${TZ:-UTC}
      - MYSQL_HOST=${MYSQL_HOST:-kodbox-db}
      - MYSQL_PORT=${MYSQL_PORT:-3306}
      - MYSQL_DATABASE=${MYSQL_DATABASE:-kodbox}
      - MYSQL_USER=${MYSQL_USER:-kodbox}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD:-kodbox123}
      - REDIS_HOST=${REDIS_HOST:-kodbox-redis}
      - REDIS_PORT=${REDIS_PORT:-6379}
      - REDIS_PASSWORD=${REDIS_PASSWORD:-}
    depends_on:
      kodbox-db:
        condition: service_healthy
      kodbox-redis:
        condition: service_healthy
    deploy:
      resources:
        limits:
          cpus: ${KODBOX_CPU_LIMIT:-2.0}
          memory: ${KODBOX_MEMORY_LIMIT:-2G}
        reservations:
          cpus: ${KODBOX_CPU_RESERVATION:-0.5}
          memory: ${KODBOX_MEMORY_RESERVATION:-256M}
    healthcheck:
      test: [CMD, wget, --no-verbose, --tries=1, --spider, 'http://localhost:80/']
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  kodbox-db:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}mysql:${MYSQL_VERSION:-9.4.0}
    environment:
      - TZ=${TZ:-UTC}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD:-root123}
      - MYSQL_DATABASE=${MYSQL_DATABASE:-kodbox}
      - MYSQL_USER=${MYSQL_USER:-kodbox}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD:-kodbox123}
    command:
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_unicode_ci
      - --default-authentication-plugin=mysql_native_password
    volumes:
      - kodbox_db_data:/var/lib/mysql
    healthcheck:
      test: [CMD, mysqladmin, ping, -h, localhost, -u, root, '-p${MYSQL_ROOT_PASSWORD:-root123}']
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    deploy:
      resources:
        limits:
          cpus: ${KODBOX_DB_CPU_LIMIT:-1.0}
          memory: ${KODBOX_DB_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${KODBOX_DB_CPU_RESERVATION:-0.25}
          memory: ${KODBOX_DB_MEMORY_RESERVATION:-256M}

  kodbox-redis:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}redis:${REDIS_VERSION:-8.2.1-alpine3.22}
    command:
      - redis-server
      - --requirepass
      - ${REDIS_PASSWORD:-}
    environment:
      - TZ=${TZ:-UTC}
    volumes:
      - kodbox_redis_data:/data
    healthcheck:
      test: [CMD, redis-cli, ping]
      interval: 5s
      timeout: 3s
      retries: 5
      start_period: 10s
    deploy:
      resources:
        limits:
          cpus: ${KODBOX_REDIS_CPU_LIMIT:-0.50}
          memory: ${KODBOX_REDIS_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${KODBOX_REDIS_CPU_RESERVATION:-0.25}
          memory: ${KODBOX_REDIS_MEMORY_RESERVATION:-128M}

volumes:
  kodbox_data:
  kodbox_db_data:
  kodbox_redis_data:
