Answers: <p>It is possible via network name:</p>
  
  <pre><code>networks:
    mynetwork:
      name: ${STAGE_NAME}
  </code></pre>
  
  <p>where mynetwork - name "inside" stack</p>
  
  <p>${STAGE_NAME} -name for other stacks/services/containers</p>
  
  <p>See comment from docker capitan <a href="https://github.com/moby/moby/issues/40819#issuecomment-618726892" rel="noreferrer">https://github.com/moby/moby/issues/40819#issuecomment-618726892</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2020-04-25T14:39:58.863Z
Answers: 2020-04-25T14:39:58.863Z
Answers: burtsevyg
Answers: <p>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</p>
  
  <pre><code>TARGET=/tmp/$STAGE_NAME
  
  mkdir -p $TARGET
  
  ln -sf $(pwd)/docker-compose.yml $TARGET/docker-compose.yml
  
  cd $TARGET
  docker-compose "$@"
  cd -
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-02-10T20:51:19.917Z
Answers: 2019-02-10T20:51:19.917Z
Answers: ivicaa
Answers: <p>There is a switch -p in docker-compose which allow you to set this prefix to a custom value.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-02-12T05:44:08.633Z
Answers: 2019-02-12T05:44:08.633Z
Answers: Krzysztof Wesołowski
Answers: <p>If you have <a href="https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html" rel="nofollow noreferrer"><code>envsubst</code></a> installed (part or gnu <code>gettext</code>) you can use something like that:</p>
  
  <pre><code>export STAGE_NAME="xyz"
  export STAGE_NETWORK_PREFIX="1.2.3.4"
  docker-compose -f &lt;(envsubst docker-compose.yml) ...
  </code></pre>
  
  <p>If your shell does not support process substitution, something like that would do the trick:</p>
  
  <pre><code>envsubst docker-compose.yml | docker-compose -f - ...
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-02-12T10:59:18.210Z
Answers: 2019-02-12T10:59:18.210Z
Answers: Sylvain Leroux
Body: <p>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.</p>
  
  <pre><code>networks:
    mynetwork.${STAGE_NAME}:
      driver: bridge
      ipam:
        driver: default
        config:
          - subnet: ${STAGE_NETWORK_PREFIX}.0/24
  </code></pre>
  
  <p>Any chance to get the network name <code>mynetwork.${STAGE_NAME}</code> configurable from outside?</p>
  
Tags: <docker-compose><environment-variables>
Title: docker-compose.yml - make network name configurable via environment variable
UserDisplayName: ivicaa
ThreadText: # docker-compose.yml - make network name configurable via environment variable
  
  (Asked by: username_0 on 2019-02-10)
  
  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?
  
  **Tags:** docker-compose, environment-variables
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_3 on 2019-02-12. Score: 4 | Accepted answer
  
  If you have [`envsubst`](https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html) 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 - ...
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2020-04-25. Score: 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>
  
  
  ---
  
  ### Answer by username_0 on 2019-02-10. Score: 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 -
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2019-02-12. Score: -1
  
  There is a switch -p in docker-compose which allow you to set this prefix to a custom value.
  
  
  ---
  
  