Docker 将配置传递给 yaml 文件

Docker pass configuration to yaml file

如何将容器名称传递给 elasticsearch.yml 中的 node.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"

因此,除了 node.name=es01,我还有什么办法可以做到 node.name=${container_nane}?

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}

所以,你需要做的是为docker-compose.yaml中的每个容器设置hostname,然后默认每个容器都会有环境变量HOSTNAME,它会自动选择通过 elasticsearch.yml:

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

并且,在 elasticsearch.yml 中,如文档中所述,您需要使用 node.name: ${HOSTNAME}