简体   繁体   English

是否可以从环境中读取 docker-compose 中的网络名称?

[英]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).我试图不对 my.network 名称进行硬编码,因为它是一个开源项目(而且我有多个实例在同一台服务器上运行,用于不同的应用程序)。

Is it possible to use environment variables when defining the.network?定义.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. Compose 有一个项目名称的内部概念,大多数 Docker object 名称都以该名称为前缀。 For example, if you are in a directory named foo and your Compose file has例如,如果您位于名为foo的目录中,并且您的 Compose 文件具有

networks:
  something:

and you run docker.network ls , you will see a.network named foo_something .然后你运行docker.network ls ,你会看到一个名为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.您可以选择要在docker-compose.yml文件中使用的任何名称,它将被限定在该文件中。

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.相反,这要求系统的不同安装要么位于具有不同名称的目录中,要么设置COMPOSE_PROJECT_NAME环境变量(可能在.env文件中),要么始终使用docker-compose -p标志。

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.在 .networks 的特定情况下,Compose提供了一个名为default的 .network,如果您实际上没有networks:块,则这是默认值。 There's not really any downside to using this, and most applications won't need multiple internal.networks.使用它并没有任何缺点,而且大多数应用程序不需要多个 internal.networks。 I'd just leave out networks: entirely.我只是忽略了networks:完全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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