Search code examples
dockerdocker-composeelastic-stack

Docker pass configuration to yaml file


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}?


Solution

  • 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}