在docker-compose中使用两个主机IP [英] Using two host IP's with docker-compose

查看:264
本文介绍了在docker-compose中使用两个主机IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将它用谷歌搜索了.

I've googled this to death.

出于各种原因,我希望一个容器绑定到一个主机IP/端口,另一个容器绑定在另一个IP/端口上.

I want one container bound to one host IP/port, the other container on a different one, for various reasons.

我有(摘录了一些信息)

I have (snipped out some info)

container1:
 network_mode: 
  host ports: 
   - 192.168.1.224:80:80

container2: 
 network_mode: 
  host ports: 
   - 192.168.1.225:80:80

Docker实际上已经启动,但是当我在浏览器中访问每个IP时,两个URL都将返回container1的内容.

Docker actually starts up, but when I visit each IP in the browser, both URL's will return container1's stuff.

有人这样做吗?我在网上可以找到的所有内容都与docker有关,而与docker-compose无关(使用某些参数启动docker),或者有人在争论它应该以另一种方式完成.

Has anyone done this? All I can find online is mostly related to docker and not docker-compose (starting docker with some arguments), or people arguing it should be done another way.

推荐答案

删除 network_mode:host 设置:在这里很麻烦.

Delete the network_mode: host setting: it's getting in your way here.

指定 network_mode:host 会绕过Docker的所有常规网络设置. ports:设置无效.这里的每个进程都可以看到您的两个主机接口,并且大概会尝试绑定到这两个接口.如果使用默认的 network_mode:网桥,则每个容器都将获得一个隔离的网络堆栈,并且可以使用 ports:选择性地将容器暴露给特定的接口.

Specifying network_mode: host bypasses all of Docker's normal networking setup. The ports: setting has no effect. Each process here sees both of your host interfaces, and presumably tries to bind to both of them. If you use the default network_mode: bridge, each container gets an isolated network stack, and you can use ports: as you've done to selectively expose containers to specific interfaces.

network_mode:host 实际上仅在几种特定情况下才适用;仅当您的服务器进程正在侦听成千上万的端口,或者其特定端口是不可预测的,或者您确实需要检查主机的网络设置但不能直接在主机上运行进程时,才如此.

network_mode: host is really only appropriate in a couple of specific cases; only if your server process is listening on thousands of ports, or its specific port is unpredictable, or if you have an actual need to inspect the host's network setup but can't run your process directly on the host.

这篇关于在docker-compose中使用两个主机IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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