Docker compose - enviroment variables contains dot

Viewed 1408

Some services eg. elasticsearch and others... have to set enviroment variables with dots eg: discovery.type=single-node

I want to put it to file, what is not a problem...

    something:
        build: './imagelocation/'
        env_file: 
          - .env

.env file contains eg.:

EL_ENVIRONMENT=development
discovery.type=single-node

and "discovery.type=single-node" is not working, I know why not working.

How to figure out this without workaround? ( and without security issue, dont want to use in docker run -e end etc )

1 Answers

I use the 3.7 docker-compose format and it works without any problems. However, remember that if docker-compose should use them automatically, you must use the env_file: property in docker-compose.yml. My service is configured as follows:

version: '3.7'
services:
  elasticsearch:
    container_name: elasticsearch
    build: elasticsearch
    image: my-elasticsearch:latest
    ports:
      - "9200:9200"
      - "9300:9300"
    env_file:
      - .env

My .env file:

ES_JAVA_OPTS=-Xmx3g -Xms3g
xpack.security.enabled=false
cluster.name=elasticsearch
discovery.type=single-node

In this case, the .env file can be named freely because we indicate its name in the docker-compose.yml file.

When starting the container, I see that Elasticsearch has started with the appropriate parameter:

elasticsearch      | [2020-04-22T06:43:36,239][INFO ][o.e.d.DiscoveryModule    ] [K_7KyC-] using discovery type [single-node]

Different approach

You can use the parameter from the .env file directly. Let's create our own parameters in the .env file (use any names):

ES_JAVA_OPTS=-Xmx3g -Xms3g
ES_XPACK_SECURITY_ENABLED=false
ES_CLUSTER_NAME=elasticsearch
ES_DISCOVERY_TYPE=single-node

...and in your docker-compose.yml file use these variables like this:

version: '3.7'
services:
  elasticsearch:
    container_name: elasticsearch
    build: elasticsearch
    image: my-elasticsearch:latest
    ports:
      - "9200:9200"
      - "9300:9300"
    environment:
      - ES_JAVA_OPTS=${ES_JAVA_OPTS}
      - xpack.security.enabled=${ES_XPACK_SECURITY_ENABLED}
      - cluster.name=${ES_CLUSTER_NAME}
      - discovery.type=${ES_DISCOVERY_TYPE}

Just remember that this time you need to have file with .env name or export this environments to your shell, e.g: export ES_XPACK_SECURITY_ENABLED=true.

Related