简体   繁体   中英

Is it possible to read network name in docker-compose from env?

I'm trying to not hard-code my.network name since its for an open source project (and I have multiple instances running on the same server for different apps).

Is it possible to use environment variables when defining the.network?

This doesn't work:

networks:
  ${DOCKER_NETWORK_NAME}:
    name: ${DOCKER_NETWORK_NAME}

Compose has an internal notion of a project name and most Docker object names are prefixed with that name. For example, if you are in a directory named foo and your Compose file has

networks:
  something:

and you run docker.network ls , you will see a.network named foo_something .

I would generally recommend not manually specifying the names of.networks, volumes, or containers. You can choose any name you want to be used within the docker-compose.yml file and it will be scoped to that file.

Conversely, this requires that different installations of the system either be in directories with different names, set the COMPOSE_PROJECT_NAME environment variable (possibly in a .env file), or consistently use the docker-compose -p flag.

In the very specific case of.networks, Compose provides a.network named default which is the default if you don't actually have networks: blocks. There's not really any downside to using this, and most applications won't need multiple internal.networks. I'd just leave out networks: entirely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM