11

I have a docker compose file with env variables for stage specific configurations. As long as env variables are used in values in docker-compose.yml everything is fine, but the problem is with the network name, since it's specified in the tag.

networks:
  mynetwork.${STAGE_NAME}:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: ${STAGE_NETWORK_PREFIX}.0/24

Any chance to get the network name mynetwork.${STAGE_NAME} configurable from outside?

4 Answers 4

4

If you have envsubst installed (part or gnu gettext) you can use something like that:

export STAGE_NAME="xyz"
export STAGE_NETWORK_PREFIX="1.2.3.4"
docker-compose -f <(envsubst docker-compose.yml) ...

If your shell does not support process substitution, something like that would do the trick:

envsubst docker-compose.yml | docker-compose -f - ...
13

It is possible via network name:

networks:
  mynetwork:
    name: ${STAGE_NAME}

where mynetwork - name "inside" stack

${STAGE_NAME} -name for other stacks/services/containers

See comment from docker capitan https://github.com/moby/moby/issues/40819#issuecomment-618726892

0

I didn't find a way to directly control this, but I noticed that the folder containing the docker-compose.yml is used as prefix when creating the name for the docker network. My solution was to write a wrapper script for docker-compose which creates a new folder with the name of the stage

TARGET=/tmp/$STAGE_NAME

mkdir -p $TARGET

ln -sf $(pwd)/docker-compose.yml $TARGET/docker-compose.yml

cd $TARGET
docker-compose "$@"
cd -
-1

There is a switch -p in docker-compose which allow you to set this prefix to a custom value.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.