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

services:
  elasticsearch:
    <<: *defaults
    image: docker.elastic.co/elasticsearch/elasticsearch-wolfi:${ELASTICSEARCH_VERSION:-9.3.0}
    ports:
      - '${ELASTICSEARCH_HTTP_PORT_OVERRIDE:-9200}:9200'
      - '${ELASTICSEARCH_TRANSPORT_PORT_OVERRIDE:-9300}:9300'
    volumes:
      - elasticsearch_data:/usr/share/elasticsearch/data
      - elasticsearch_logs:/usr/share/elasticsearch/logs

      # Custom configuration
      # - ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml:ro
    environment:
      - TZ=${TZ:-UTC}
      - node.name=elasticsearch
      - cluster.name=${ELASTICSEARCH_CLUSTER_NAME:-docker-cluster}
      - discovery.type=${ELASTICSEARCH_DISCOVERY_TYPE:-single-node}
      - bootstrap.memory_lock=true
      - xpack.security.enabled=${ELASTICSEARCH_SECURITY_ENABLED:-false}
      - xpack.security.http.ssl.enabled=${ELASTICSEARCH_SSL_ENABLED:-false}
      - xpack.security.transport.ssl.enabled=${ELASTICSEARCH_SSL_ENABLED:-false}
      - 'ES_JAVA_OPTS=-Xms${ELASTICSEARCH_HEAP_SIZE:-1g} -Xmx${ELASTICSEARCH_HEAP_SIZE:-1g}'
    ulimits:
      memlock:
        soft: -1
        hard: -1
    deploy:
      resources:
        limits:
          cpus: ${ELASTICSEARCH_CPU_LIMIT:-2.00}
          memory: ${ELASTICSEARCH_MEMORY_LIMIT:-2G}
        reservations:
          cpus: ${ELASTICSEARCH_CPU_RESERVATION:-0.50}
          memory: ${ELASTICSEARCH_MEMORY_RESERVATION:-1G}
    healthcheck:
      test:
        [CMD-SHELL, 'curl -f http://localhost:9200/_cluster/health || exit 1']
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s

volumes:
  elasticsearch_data:
  elasticsearch_logs:
