如何在版本2的docker-compose定义文件中定义网络? [英] How do you define a network in a version 2 docker-compose definition file?
问题描述
关于网络的文档目前是非常模糊的 - 如何您将完成一个虚拟网络的 在这种情况下,目标不是依赖于预定义的网络,而是为docker-compose定义的容器集合。而是将Docker-compose定义文件中的网络定义自包含在内。 使用预定义的网络,如果 但是我的观点是在docker-compose配置中定义一个网络,而不是假设一个是先验定义的... 自动创建默认网络。请参阅 https://docs.docker.com/compose/networking/ 的开始部分 Compose将创建一个 您可以在撰写文件的顶层创建 The documentation about networking is currently very vague on this ― how do you accomplish a Goal in this scenario being not relying on a pre-defined network, for an ensemble of containers defined for docker-compose. Rather have the network definition self-contained in the docker-compose definition file. With a pre-defined network, this below would work if the application in But my point is defining a network inside the docker-compose configuration, not assuming one was a-priori defined... A default network is automatically created. See the beginning section of https://docs.docker.com/compose/networking/ for how to address containers within this network. Compose will create a You can create a 这篇关于如何在版本2的docker-compose定义文件中定义网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! docker-compose.yml
,让其定义的服务(容器)在该网络上通信? p>
A
使用名称 B
作为主机名,用于访问打包在 B
监听其端口9000. 主机:端口
它将使用它将是 B:9000
(更具体地说uri mongodb:// B:9000
在我特定的情况下)
foo:
net:my-pre-defined-network
container_name:A
image:foo
bar:
net:my-pre-defined-network
container_name:B
image:bar
ports:
- 9000:9000
TL; DR
默认值
网络,只要您使用版本2格式,但如果您想自定义文档在这里的网络:
网络
部分,并在每个网络
部分中引用它们服务。但是您不需要,只需使用下面的注释中所述的默认网络。 docker-compose.yml
that creates a virtual network, letting the services (containers) defined by it communicate on that network?A
used the name B
as the hostname for accessing the application packaged inside B
listening on its port 9000. The host:port
it would use for it would be B:9000
(more specifically the uri mongodb://B:9000
in my particular case).foo:
net: my-pre-defined-network
container_name: A
image: foo
bar:
net: my-pre-defined-network
container_name: B
image: bar
ports:
- "9000:9000"
TL;DR
default
network for you as long as you use the version 2 format, but if you'd like to customize the networks the docs are here:
networks
section at the top level of the Compose file and reference them in the networks
section of each service. But you don't need to, just use the default network as described in the comments below.