Windows上的Docker:如何使用容器IP从主机连接到容器? [英] Docker on Windows: how to connect to container from host using container IP?

查看:522
本文介绍了Windows上的Docker:如何使用容器IP从主机连接到容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有


  • Windows 10

  • 适用于Windows V的Docker。1.12.5修订版。 9503,它不再依赖boot2docker或VirtualBox。

我在Docker映像中运行了许多JBoss实例(172.18.0.2是数据库):

I have a number of JBoss instances running in Docker images (172.18.0.2 is a database):


  • 实例1:名称:jboss-eap,IP:172.18.0.3

  • 实例2:名称:jboss-eap-arquillian,IP:172.18.0.4

应同时运行。每个JBoss实例导出例如它的8787、8080和8443端口。

that shall be running at the same time. Each JBoss instance exports e.g. its 8787, 8080 and 8443 ports.

我还创建了一个桥接网络:
docker network create --driver bridge --subnet 172.18.0.0/24 bridged_network

I have also created a bridged network: docker network create --driver bridge --subnet 172.18.0.0/24 bridged_network

当前,我已经设置了本地端口转发,以便主机可以使用前缀访问各种服务(例如,访问实例1的端口8080时,主机使用本地主机:28080进行连接)。

Currently, I have set up a local port forwarding such that the host can access the various services using a prefix (e.g. when accessing instance 1's port 8080, the host uses localhost:28080 to connect). But that's quite error prone.

现在,我想使用容器的IP从主机访问那些端口。调用172.18.0.4:8080(下一步:使用主机名:jboss-eap-arquillian:8080)。
尽管这在各个容器之间都可以顺利进行,但是我无法将其设置为从主机进行连接。

Now, I want to access those ports from the host using the container's IP, e.g. calling 172.18.0.4:8080 (next step: using the host name: jboss-eap-arquillian:8080). While this is working smoothly from container to container, I haven't been able to set it up to connect from the host.

有Windows网络接口(类型:DockerNAT),具有IP 10.0.75.1,但是我可能是手动创建的(不确定...现在已经尝试了一段时间)。但这在将容器的端口绑定到此设备时很有用,例如docker run ... -p 10.0.75.1:8080:8080。然后,我可以使用10.0.75.1:8080调用该服务,但这对第二个容器没有帮助。

There is Windows network interface (type: DockerNAT), having IP 10.0.75.1, but it's possible that I've created it manually (unsure ... been trying for quite some time now). But that is helpful when binding the container's port to this device, e.g. docker run ... -p 10.0.75.1:8080:8080. I can then call the service using 10.0.75.1:8080, but that does not help me for the 2nd container.

我也尝试使用Docker的主机网络设备,但
a。似乎只适用于一台机器
b。我无法静态设置IP,这是运行静态配置的JUnit测试所必需的。
注意:启动后,我不能依靠网络DHCP来分配IP,因为我经常更改网络(因此也更改了DHCP),从而又导致IP不固定。

I've also tried to use the Docker's host network device, but that a. seems to work for only one machine b. I cannot statically set the IP, which is needed for JUnit tests running a static configuration. Note: I cannot rely on the network DHCP to assign an IP upon startup, since I change my network (and therefore the DHCP) frequently, resulting in an unfixed IP again.

因此,基本上,我正在寻找一种以这种方式设置网络/容器的方法,以便可以使用容器IP(例如172.18)调用容器(例如在端口8080上)提供的服务。 0.3 for example 1)from host using 172.18.0.3:8080。

So basically I'm looking for a way to set up the network/container in such a way, that I can call the service provided by the container (e.g. on port 8080) using the container IP (e.g. 172.18.0.3 for instance 1) from the host using 172.18.0.3:8080.

推荐答案

在Windows的docker上,您可以使用地址10.0。 75.1:8080,但是您需要配置防火墙,更好的方法是使用地址10.0.75.2:8080

On docker for windows you can use address 10.0.75.1:8080 but you need to configure your firewall, a better way is using address 10.0.75.2:8080,

这两个地址都必须在运行时发布端口容器

for both addresses you have to publish your port when you run container

docker run -p 8080:8080 image_name

更多信息 https:/ /github.com/docker/for-win/issues/334#issuecomment-297030101

这篇关于Windows上的Docker:如何使用容器IP从主机连接到容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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