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

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

问题描述

我有

  • Windows 10
  • Docker for Windows V. 1.12.5 Rev. 9503,不再依赖 boot2docker 或 VirtualBox.

我有许多 JBoss 实例在 Docker 镜像中运行(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 时,主机使用 localhost: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,但有可能是我手动创建的(不确定……已经尝试了很长时间了).但这在将容器的端口绑定到此设备时很有用,例如码头工人运行... -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 的主机网络设备,但是一个.似乎只适用于一台机器湾我无法静态设置 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.0.3 调用容器提供的服务(例如端口 8080)实例 1) 来自使用 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.

推荐答案

在 docker for windows 上你可以使用地址 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天全站免登陆