version: '3.8'

services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:9.0.0
    container_name: elasticsearch
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=true
      - ELASTIC_PASSWORD=Elastic123!
      - ES_JAVA_OPTS=-Xms1g -Xmx1g
    ulimits:
      memlock:
        soft: -1
        hard: -1
    ports:
      - 9200:9200
    networks:
      - elastic
    healthcheck:
      test: curl -s -u elastic:Elastic123! http://localhost:9200/_cluster/health | grep -q '"status":"green"' || exit 1
      interval: 10s
      retries: 20
    volumes:
      - esdata:/usr/share/elasticsearch/data

  kibana:
    image: docker.elastic.co/kibana/kibana:9.0.0
    container_name: kibana
    depends_on:
      elasticsearch:
        condition: service_healthy
    environment:
      - ELASTICSEARCH_HOSTS=http://elasticsearch:9200
      - ELASTICSEARCH_USERNAME=kibana_system
      - ELASTICSEARCH_PASSWORD=Kibana123!
    ports:
      - 5601:5601
    networks:
      - elastic
    entrypoint: >
      bash -c '
        until curl -s -u elastic:Elastic123! -X GET http://elasticsearch:9200 >/dev/null; do
          sleep 1
        done

        curl -s -u elastic:Elastic123! -X POST http://elasticsearch:9200/_security/user/kibana_system/_password \
          -H "Content-Type: application/json" \
          -d "{\"password\": \"Kibana123!\"}"

        /bin/tini -- /usr/local/bin/kibana-docker
      '

volumes:
  esdata:

networks:
  elastic:
    driver: bridge
