Docker Compose 同时使用主机和桥接网络

Con*_*ham 7 docker docker-compose docker-network

我有 2 个图像,我想通过 compose 的默认桥接网络相互通信,但我也希望它们能够访问我的主机网络。

为了允许图像在 compose 的默认桥接网络中相互通信,我不需要执行任何操作:

version: '3.4'
services: 
  hello:
   image: hello-world

  world:
    image: hello-world
Run Code Online (Sandbox Code Playgroud)

通过上述规范,服务可以通过使用 DNS 名称来hello引用服务worldworld

如果我希望 hello 服务能够与我的主机网络交互,我可以添加network_mode: host到规范中。

version: '3.4'
services: 
  hello:
   image: hello-world
   network_mode: host
  world:
    image: hello-world
Run Code Online (Sandbox Code Playgroud)

这允许hello服务访问我的主机网络,但无法再world通过 compose 的内置 DNS 进行访问。我怎样才能同时实现这两个目标?

我尝试创建自定义网络,但自定义网络无法使用主机驱动程序,并且我无法network_mode与以下命令结合使用networks

这不起作用:

version: '3.4'
services: 
  hello:
   image: hello-world
   network_mode: host
   networks:
   - bridge
  world:
    image: hello-world
    networks:
    - bridge

networks:
  test:
    driver: bridge
Run Code Online (Sandbox Code Playgroud)
$ docker-compose up
ERROR: 'network_mode' and 'networks' cannot be combined
Run Code Online (Sandbox Code Playgroud)

Ita*_*ing 0

如果它们都在您的主机网络上,则它们可以像普通应用程序相互通信一样相互通信:通过本地主机。所以你根本不需要这座桥。