How to unbind some environment fields in docker compose when restarting the container?


I have an elastic search cluster running on three different hosts. Here is my compose file:

version: '3.7'
    container_name: ${NODE_NAME}
    #  - elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - ${PWD}/certs:/usr/share/elasticsearch/config/certs
      - esdata03:/usr/share/elasticsearch/data
      - 9200:9200
      - 9300:9300
    environment: ${NODE_NAME} ${CLUSTER_NAME}
      # http.publish_host: ${HOST_IP} #new #will not effect the hand shake fail
      cluster.initial_master_nodes: es-sim01,es-sim02,es-sim03
      discovery.seed_hosts: ${DISCOVERY_SEED}
      network.bind_host: ${NODE_NAME}
      network.publish_host: ${HOST_IP} #new
      transport.profiles.default.port: 9300 #new
      bootstrap.memory_lock: true true true true certs/${NODE_NAME}/${NODE_NAME}.key certs/${NODE_NAME}/${NODE_NAME}.crt certs/ca/ca.crt true certs/${NODE_NAME}/${NODE_NAME}.key certs/${NODE_NAME}/${NODE_NAME}.crt certs/ca/ca.crt certificate
      xpack.license.self_generated.type: ${LICENSE}
      # ${HOST_IP}
    mem_limit: ${MEM_LIMIT}
        soft: -1
        hard: -1

    driver: local
    driver: local

So the thing is that “cluster.initial_master_nodes: es-sim01,es-sim02,es-sim03” should not be used after the first bootstrap of the elastic search cluster. So in case one of my instances stopped/exited and I want to restart that container without the “cluster.initial_master_nodes” variable. What should I do?

Hi~ Anyone could help? :pray: :pray: :pray:

There is no way to use an environment variable in a container and remove it just by restarting it. You can however remove the value from the compose file and run docker compose up -d again. When something is required only at the first start, you can create two compose files. One with the values that are always needed and one that contains values for the initialization. Than run the first compose command like this:

docker compose -f compose.yml -f compose-init.yml up -d

After the initialization you can run just docker compose up -d again.

I am curious, why do you think that the mentioned variable should not be defined after the initialization? I haven’t installed Elasticsearch for a while, but even the official documentation mentions that value in the compose file and I would find it strange if the Elastic team created an image for which you need two separate commands to start properly. If they did, you could report it to them somewhere.

1 Like

Hi @rimelek

Thanks for your reply. I think this command will resolve my problem I will give it a try.

I am new to Elasticsearhc, so I basically just follow their documentation and try it out by myself. And there is the place I found and I believe I need to remove the mentioned variable after the cluster has started for the first time.

I see. Thank you for sharing the source. You were right. It seems that configuration is not for production environments.

1 Like