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

Hi,

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

version: '3.7'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
    container_name: ${NODE_NAME}
    volumes:
    #  - elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - ${PWD}/certs:/usr/share/elasticsearch/config/certs
      - esdata03:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
      - 9300:9300
    environment:
      node.name: ${NODE_NAME}
      cluster.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}
      ELASTIC_PASSWORD: ${ELASTIC_PASSWORD}
      # network.host: 0.0.0.0
      network.bind_host: ${NODE_NAME}
      network.publish_host: ${HOST_IP} #new
      transport.profiles.default.port: 9300 #new
      bootstrap.memory_lock: true
      xpack.security.enabled: true
      xpack.security.http.ssl.enabled: true
      xpack.security.enrollment.enabled: true
      xpack.security.http.ssl.key: certs/${NODE_NAME}/${NODE_NAME}.key
      xpack.security.http.ssl.certificate: certs/${NODE_NAME}/${NODE_NAME}.crt
      xpack.security.http.ssl.certificate_authorities: certs/ca/ca.crt
      xpack.security.transport.ssl.enabled: true
      xpack.security.transport.ssl.key: certs/${NODE_NAME}/${NODE_NAME}.key
      xpack.security.transport.ssl.certificate: certs/${NODE_NAME}/${NODE_NAME}.crt
      xpack.security.transport.ssl.certificate_authorities: certs/ca/ca.crt
      xpack.security.transport.ssl.verification_mode: certificate
      xpack.license.self_generated.type: ${LICENSE}
      # network.host: ${HOST_IP}
    mem_limit: ${MEM_LIMIT}
    ulimits:
      memlock:
        soft: -1
        hard: -1

volumes:
  certs:
    driver: local
  esdata03:
    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