在Windows 10中使用Docker for Windows将容器端口公开给主机 [英] Expose container port to Host using Docker for Windows in Windows 10

查看:634
本文介绍了在Windows 10中使用Docker for Windows将容器端口公开给主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 docker API 并尝试创建一个简单的容器并公开1个端口。

I've reading docker API and trying to create a simple container and expose 1 port.

在我的示例中,我有一个应用程序监听 TCP 端口 9595 放在容器中,我想从外部世界(即容器的主机)在端口 9090 进行访问。

In my example, I have an application that listen on TCP port 9595 inside the container and I want to access it from outside world (i.e. the host of the container) on port 9090.

创建容器时,我有 ExposedPorts:{ 9595 / tcp:{}} PortBindings:{ 9595 / tcp:[{ HostPort: 9090}]}

因此,如果我访问从主机 http:// container_internal_ip:9595 可以正常工作!但是,当我访问 http:// localhost:9090 时(使用此端口映射功能,这是我期望的结果),它不起作用...

So, if I access from the Host machine http://container_internal_ip:9595 it works! However, when I access http://localhost:9090, which is what I expect using this port map feature, it doesn't work...

运行 docker port containerID 时,我有 9595 / tcp-> 0.0.0.0:9090 ,这意味着当连接到主​​机上的任何IP时,在端口9090处转发到端口9595处的容器

While running docker port containerID I have 9595/tcp -> 0.0.0.0:9090 and that should means, when connecting to any IP on the host, at port 9090, forward to the container in port 9595.

那么,这是怎么了?为什么我不能连接到9090?

So, what is wrong here? Why can't I connect to 9090?

我感谢您的澄清。

推荐答案

该端口确实正按预期方式暴露于外部世界,由于当前的Windows网络限制,该端口恰巧无法从环回接口(本地主机或127.0.0.1)上的容器主机本身访问。与其尝试通过容器主机上的环回接口访问它,不如使用容器主机ip和容器暴露的端口号从网络上的另一台计算机上访问它。

The port really is being exposed to the 'outside world' as expected, it just happens to be inaccessible from the container host machine itself on the loopback interface (localhost or 127.0.0.1) due to a current windows networking limitation. Rather than trying to access it via the loopback interface on the container host, try to access it from another machine on the network, using the container host ip and the containers exposed port number.

https://blog.sixeyed.com/published-ports-on-windows-containers-dont-do-loopback/

这篇关于在Windows 10中使用Docker for Windows将容器端口公开给主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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