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

NetworkingDockerWindows 10Virtual MachineHyper V

Networking Problem Overview


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?

Networking Solutions


Solution 1 - Networking

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.

Solution 2 - Networking

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).

Solution 3 - Networking

The host will have a host.docker.internal registered in the default DNS used by containers. So you can use something like curl http://host.docker.internal/ to access a web server running on your machine even if that server is running in another container provided you exposed the port.

Solution 4 - Networking

Just for adding a note for docker toolbox users. Short answer is: 192.168.56.1

enter image description here

enter image description here

Solution 5 - Networking

On Windows 10, after your docker container is started, you can run docker-machine ip in command line (cmd or Docker QuickStart Terminal, etc) to get the ip address of your docker container. This ip address is usually, 192.168.99.100.

Solution 6 - Networking

I'm using Windows containers with Docker version 20.10.11. I'm trying to reach a server running on the host machine. I tried all other answers/comments. None are working for me. I tried...

  • 10.0.75.1
  • 10.0.0.2
  • launching with --net="host" (for docker-compose, "network_mode: host")
  • host.docker.internal
  • docker.for.win.localhost
  • 192.168.56.1
  • 192.168.99.100
  • The IP of every vEthernet adapter on the host

The only way I can communicate is using the standard ethernet adapter's IPv4 on the host (not the container):

enter image description here

Using curl http://10.0.0.4:8080 accesses the server just fine.

Side note - running a config script beforehand would also solve the problem... just shouldn't be necessary. https://stackoverflow.com/a/67434367/7991646

Solution 7 - Networking

It's so strange that in 2020 April, this is still a question. and most of the "host.docker.internal" and "172.17.0.1" don't work for WINDOWS docker.

so, I suggest for windows docker user, just simple type "ipconfig" in cmd :

and you will get some ips for your windows (host ) machine:

enter image description here

then ,in your docker, install "ping/curl/ifconfig/telnet" , then type "ifconfig" to get your "docker" ip address, then type "ping

this needs you install external tools to docker, but it worth.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRemiXView Question on Stackoverflow
Solution 1 - NetworkingRemiXView Answer on Stackoverflow
Solution 2 - Networkingtomas.zigardiView Answer on Stackoverflow
Solution 3 - NetworkingArchimedes TrajanoView Answer on Stackoverflow
Solution 4 - Networkinguzay95View Answer on Stackoverflow
Solution 5 - NetworkingJohnsonView Answer on Stackoverflow
Solution 6 - NetworkingJeremy BealeView Answer on Stackoverflow
Solution 7 - NetworkingSiweiView Answer on Stackoverflow