Expose container port to Host using Docker for Windows in Windows 10

假如想象 提交于 2019-12-18 19:19:55

问题


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

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.

When creating the container I have "ExposedPorts": { "9595/tcp: {}" } and the "PortBindings": { "9595/tcp": [{ "HostPort": "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...

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.

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

I appreciate any clarifications.


回答1:


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.

There is a good writeup for this at https://blog.sixeyed.com/published-ports-on-windows-containers-dont-do-loopback/




回答2:


Please post the docker command you are using.

You need an special treatment if you use Docker Toolbox or Docker for Windows. You should see localhost without problems (if your container is Linux). If you want your container be visible from outside, try this with Elevated Power Shell:

netsh interface portproxy add v4tov4 listenaddress=yourip listenport=9090 connectaddress=10.0.75.1 connectport=9090

If your container is Windows however you won't see localhost and you need to use the container internal address:

docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" <cointainerid>

Reference: https://blog.docker.com/2016/09/build-your-first-docker-windows-server-container/

Regards



来源:https://stackoverflow.com/questions/40349893/expose-container-port-to-host-using-docker-for-windows-in-windows-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!