如何在版本2的docker-compose定义文件中定义网络? [英] How do you define a network in a version 2 docker-compose definition file?

查看:148
本文介绍了如何在版本2的docker-compose定义文件中定义网络?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于网络的文档目前是非常模糊的 - 如何您将完成一个虚拟网络的 docker-compose.yml ,让其定义的服务(容器)在该网络上通信? p>

在这种情况下,目标不是依赖于预定义的网络,而是为docker-compose定义的容器集合。而是将Docker-compose定义文件中的网络定义自包含在内。



使用预定义的网络,如果 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

但是我的观点是在docker-compose配置中定义一个网络,而不是假设一个是先验定义的...



TL; DR



自动创建默认网络。请参阅 https://docs.docker.com/compose/networking/ 的开始部分

解决方案

Compose将创建一个默认值网络,只要您使用版本2格式,但如果您想自定义文档在这里的网络:





您可以在撰写文件的顶层创建网络部分,并在每个网络部分中引用它们服务。但是您不需要,只需使用下面的注释中所述的默认网络。


The documentation about networking is currently very vague on this ― how do you accomplish a docker-compose.yml that creates a virtual network, letting the services (containers) defined by it communicate on that network?

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 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"

But my point is defining a network inside the docker-compose configuration, not assuming one was a-priori defined...

TL;DR

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 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:

You can create a 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.

这篇关于如何在版本2的docker-compose定义文件中定义网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆