1

How can I pass container name to node.name in elasticsearch.yml rather than duplicating it to all service name

version: '2.2'
services:
  es01:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.14.0
    container_name: es01
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - type: bind
        source: ./elasticsearch.yml
        target: /usr/share/elasticsearch/config/elasticsearch.yml
      - data01:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - elastic

  es02:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.14.0
    container_name: es02
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - type: bind
        source: ./elasticsearch.yml
        target: /usr/share/elasticsearch/config/elasticsearch.yml
      - data02:/usr/share/elasticsearch/data
    networks:
      - elastic

networks:
  elastic:
    driver: bridge

elasticsearch.yml

node.name: es01
cluster.name: es-docker-cluster
discovery.seed_hosts: es02,es03
cluster.initial_master_nodes: es01,es02,es03
bootstrap.memory_lock: true
ES_JAVA_OPTS: "-Xms512m -Xmx512m"

Therefore, instead of having node.name=es01 is there a way i can do node.name=${container_nane}?

navin
  • 55
  • 1
  • 6
  • I'm not sure if I'm understanding this correctly. First of all, you're using the same `elasticsearch.yml` file for everything so `node.name` will be the same for all services. Second, there's no way for Docker to make changes to an external file. What you're aiming for can be done but requires scripting but that is outside Docker. – jackeblagare Aug 11 '21 at 05:55

1 Answers1

0

See elasticsearch Environment variable substitution:

Environment variables referenced with the ${...} notation within the configuration file will be replaced with the value of the environment variable. For example:

node.name:    ${HOSTNAME}
network.host: ${ES_NETWORK_HOST}

So, what you need to do is set hostname for every container in docker-compose.yaml, then by default every container will have the environment variable HOSTNAME, and it will automatically picked by elasticsearch.yml:

version: '2.2'
services:
  es01:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.14.0
    container_name: es01
    hostname: es01

And, in elasticsearch.yml, as mentioned in doc, you need to use node.name: ${HOSTNAME}

atline
  • 28,355
  • 16
  • 77
  • 113
  • can i know does it need be in capital or can follow same as docker compose? – navin Aug 11 '21 at 07:52
  • In `docker-compose.yaml`, use `hostname`, while in `elasticsearch.yml`, use `${HOSTNAME}`. – atline Aug 11 '21 at 07:53
  • hi, how i can troubleshoot it is picking correct value ya? i tried docker exec check elasticsearch.yml not working – navin Aug 11 '21 at 09:43
  • `elasticsearch.yaml` won't be changed, the replacement happens on the fly. So you could confirm it with `docker logs your_container_id | grep node\.name`, in the startup log of that container, you could find `node.name` info, check it. – atline Aug 11 '21 at 09:53
  • [The default `node.name` is the hostname](https://www.elastic.co/guide/en/elasticsearch/reference/current/important-settings.html#node-name) and you wouldn't need to manually set it in the config file. – David Maze Aug 11 '21 at 10:56