How to connect to docker host from container on Windows 10 (Docker for Windows)

回眸只為那壹抹淺笑 提交于 2019-12-17 05:50:08

问题


At which IP address can a docker container connect to its host on Docker for Windows (on Windows 10)? How do you find this IP address?

Example: you have a service running at port 1234 on your Windows 10 machine. A program inside your container must access this service. What IP address should the program use to connect to the host?


回答1:


Short answer: in most cases, you'll need 10.0.75.1 .

In Docker for Windows, the container communicates through a vEthernet adapter called DockerNAT. To find its details, open Command Prompt and type

ipconfig

Look for an entry that looks like

Ethernet adapter vEthernet (DockerNAT):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::fd29:297:4583:3ad4%4
   IPv4 Address. . . . . . . . . . . : 10.0.75.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :

The IP address to the right of IPv4 Address is the one you need.

Note: make sure the service allows connections from outside your host. As far as that service is concerned, your docker container is a different machine. Also make sure Windows Firewall allows communication to and from the service.




回答2:


One of options that allows you to connect from container to host, is to run your container with parameter

--net="host"

Example:

docker run -it --net="host" container_name

Then from container, you can connect to service on host using:

localhost:port

But in this case, you will not be able to link more containers using --link parameter.

More on this topic: http://phillbarber.blogspot.sk/2015/02/connect-docker-to-service-on-parent-host.html

UPDATE:

From version 18.03, you can use DNS name host.docker.internal, which resolves to the internal IP address used by the host.

More: https://docs.docker.com/docker-for-windows/networking/

On older versions, you can connect to service running on host Windows using IP address you get executing command ipconfig on host -> Ethernet adapter -> IPv4 Address

UPDATE As per Datz comment below, docker.for.win.localhost is working in Docker for Windows (confirmed).



来源:https://stackoverflow.com/questions/40746453/how-to-connect-to-docker-host-from-container-on-windows-10-docker-for-windows

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