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

9

101

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?

Pound answered 22/11, 2016 at 16:3 Comment(3)
In some cases it's 10.0.0.2Octameter
Hello, your container is Windows or Linux?Lindyline
See this link to be able to access Windows host from Linux containersBunker
P
105

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.

Pound answered 22/11, 2016 at 16:3 Comment(8)
could you expand on how to allow connections from outside your host?Bridgework
This depends on the service you want to connect to. Most services have a configuration file in which you can specify an IP (range) that is allowed to connect to your service.Pound
What if I want to get the IP dinamically, for instance by domain name ( ping host)? I'm trying to deploy my app in Azure (Windows Server 2016 with Containers) wich doesn't get 10.0.75.1.Endothelioma
and when I have multiple containers running?Irritant
I tried to connect from my docker container to the windows host using the DockerNAT ip.. It doesn't work. If this answer was working before, something in docker for windows has changed..Dramatization
@Dramatization from what I've found, DockerNAT is used by Linux containers. In case if you switched your Docker to use "Windows containers" then you have to check the IP address of vEthernet adapter which is called natOverfeed
This solution no longer works in Docker for Windows v2.2.0 -- they completely removed the DockerNAT adapter, and 10.0.75.1 is no longer a valid IP address mapping to the host. See github.com/docker/for-win/issues/5538Steric
This information is completely outdated and does not work with latest docker.Decision
T
62

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

Thais answered 24/11, 2016 at 15:5 Comment(7)
As I understand from my attempts and this answer, --net="host" does not enable port mapping (say port 80 to 8000) and can cause conflicts between port numbers on the host and those in the Docker container (e.g. if both the host and the container have Apache running on port 80).Pound
Your answer does not work for Windows and Mac atm. --net="host" only works for Linux. On Mac you can access host Machine by name docker.for.mac.localhost, on Windows there is no such name and you can get bridge IP address with ipconfigBine
Interesting; I'm running 18.03 on Windows, and host.docker.internal fails to resolve inside the container.Martell
The Docker for windows host now does have the DNS name docker.for.win.localhostSundae
To me, the DNS name host.docker.internal can be accessed but docker.for.win.localhost cannot. So due to this link that is most updated (Closed) Container can not resolve docker.for.win.localhost, We might call to the host.docker.internal for the default DNS name.Mcgannon
docker.for.win.localhost only works when i use it from linux container. It doesn't work from windows containerNickell
The name host.docker.internal works in the latest Docker Desktop for Windows (4.8.2)Mickiemickle
T
27

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.

Taskmaster answered 15/4, 2020 at 12:2 Comment(1)
not always work in my Linux&Windows machineBetsybetta
B
11

It's so strange that in 2022 April, this is still a question. and most of the host.docker.internal and 172.17.0.1 do NOT 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's" ip address, then type ping <WINDOWS_HOST_IP> to see if the ip is correct.

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

Betsybetta answered 28/4, 2022 at 6:50 Comment(1)
This was the best worded information that helped me find the solution. use ipconfig to find the virtual adapter that docker is using (for me it's called WSL) The actual name and IP are very different to any of the posts here, so there are no short answers that will work for everyone.Cressi
I
4

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

enter image description here

enter image description here

Indoxyl answered 22/11, 2016 at 16:3 Comment(0)
J
2

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.

Jiva answered 1/6, 2020 at 5:43 Comment(0)
R
2

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://mcmap.net/q/46926/-is-anyone-else-having-trouble-using-host-docker-internal-in-azure-pipelines

Rope answered 23/1, 2022 at 2:52 Comment(0)
G
1

open Command Prompt and type ipconfig

look for

IPv4 Address. . . . . . . . . . . : 192.xxx.0.xxx

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

OR

"host" : "host.docker.internal"

also work

Gleich answered 11/8, 2022 at 8:34 Comment(0)
M
0

I had the similar issue. In my case /etc/hosts file was missing from my container. This was because I was not using docker desktop. Docker desktop allows docker deamon to write this entry in /etc/hosts file. I was using docker engine with lcow on windows server 2019. I simply had to use my host machine IP instead of host.docker.internal to access the services on my host machine. I could use this IP since it was static. In case of dynamic IP you will need dns to resolve that IP or use bridge network IP. See the details of the issue and how I fixed it on this thread: Unable to resolve docker.host.internal with docker-compose on windows server 2019

Marquismarquisate answered 11/8, 2022 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.