5

How can I use variable substitution for a list, map, or array value in a docker-compose.yml file.

For example:

graylog:
  image: graylog2/server2
  extra_hosts: ${EXTRA_HOSTS}

and

export EXTRA_HOSTS="['host1:10.10.10.1','host2:10.10.10.2']"

gives the following error: graylog.extra_hosts must be a mapping

I've tried different variations of the above with no luck.

I do see that there's an open issue about this here: https://github.com/docker/compose/issues/4249

Is it just not possible? Does anyone know of a workaround?

1 Answer 1

7

At least as of this time (June 2018), Docker still doesn't support this. I was able to work around the issue utilizing envsubst.

envsubst is part of gettext and it can be used to replace only environment variables you tell it to.

Tweak the docker-compose.yml value to look like an array or map (either brackets or curly braces) but have the value be an environment variable.

For example

graylog:
  image: graylog2/server2
  extra_hosts: [ ${EXTRA_HOSTS} ]

Then, define your environment variable without brackets or curly braces.

For example:

export EXTRA_HOSTS="'host1:10.10.10.1','host2:10.10.10.2'"

Then utilize envsubst

envsubst '${EXTRA_HOSTS}' < docker-compose.yml > docker-compose.subst.yml && docker stack deploy -c docker-compose.subst.yaml foobar

Notice that you pass '${EXTRA_HOSTS}' to envsubst. This tells it to only replace this environment variable. This ensures it doesn't accidentally replace some other variable that's utilizing the variable substitution syntax of Docker compose files.

Sign up to request clarification or add additional context in comments.

3 Comments

Any news about this feature without having to use envsubst?
docker-compose is primary meant for testing purpose. Better use docker run with proper arguments, and associative arrays in a bash script if required.
Eesh, no, docker compose is for production and portability - docker run is for testing. You're doing things the wrong way around.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.