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

services:
  valkey:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}valkey/valkey:${VALKEY_VERSION:-8.0-alpine}
    ports:
      - '${VALKEY_PORT_OVERRIDE:-6379}:6379'
    volumes:
      - valkey_data:/data
      # Use a custom valkey.conf file
      # - ./valkey.conf:/etc/valkey/valkey.conf
    environment:
      - TZ=${TZ:-UTC}
    command:
      - sh
      - -c
      - |
        if [ -n "$${VALKEY_PASSWORD}" ]; then
          valkey-server --requirepass "$${VALKEY_PASSWORD}" --appendonly yes
        else
          valkey-server --appendonly yes
        fi
    deploy:
      resources:
        limits:
          cpus: ${VALKEY_CPU_LIMIT:-0.5}
          memory: ${VALKEY_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${VALKEY_CPU_RESERVATION:-0.1}
          memory: ${VALKEY_MEMORY_RESERVATION:-256M}
    healthcheck:
      test: [CMD, valkey-cli, ping]
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 5s

volumes:
  valkey_data:
