I need to connect to docker host from a linux container in my windows server machine. But docker dns is not able to resolve docker.host.internal. I have tried to add host.docker.internal in docker compose extra_hosts tag and have also tried to map my machine ip with this. But none of this is working.
extra_hosts:
- "host.docker.internal:host-gateway"
extra_hosts:
- "host.docker.internal:my_ip"
I have also tried to check the /etc/hosts file in my container and its is not there. Since the hosts file is not there so docker dns cannot resolve host.docker.internal.
docker exec my_container_id cat /etc/hosts
/etc/hosts: No such file or directory
My Environment:
- Host Machine: Windows Server 2019
- docker version: 20.10.10
- lcow version: v4.14.35-v0.3.9
- docker-compose version: v2.9.0
I have also tried to update my docker version to 20.10.11 which is working fine on my windows 10 machine.
PS C:\Windows\system32> docker version
Client:
Version: 20.10.11
API version: 1.41
Go version: go1.16.10
Git commit: dea9396
Built: Thu Nov 18 00:42:51 2021
OS/Arch: windows/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.11
API version: 1.41 (minimum version 1.24)
Go version: go1.16.9
Git commit: 847da18
Built: Thu Nov 18 00:38:11 2021
OS/Arch: windows/amd64
Experimental: true
I am unable to understand why docker is unable to create /etc/hosts file.
I have made this work by adding a manual entry in /etc/hosts file. Since I was not using docker desktop so docker deamon was not able to create /etc/hosts file inside my container. And due to this missing file my container was not able to connect to host.docker.internal. I made sure few things before doing this.
I checked if bridge network was created (from my host machine):
Then I inspected the nat network which is the default bridge network created by docker deamon:
Here the bridge IP is 172.31.80.1
Next I verified if this IP is accessible inside my container. Note that I had to use ubuntu container to verify this. As containers only have basic commands so I pulled the latest ubuntu image from dockerhub and installed ping, ip and route commands on it to verify the connectivity of container with host.
Inside my ubuntu container I typed route command to check the IP table
In the IP route table I can see the bridge IP as default route. This entry verifies that my container can communicate with the docker bridge network. I can either use this IP to access any service on my host machine or my host machine IP (I used the host machine IP as this was static, in case of dynamic IP prefer to use bridge network IP or dns)
Now inside my ubuntu container I can ping bridge IP and my host machine IP both:
Note that 10.25.241.37 is the IPv4 address of my Ethernet0 adapter, you can check this by typing ipconfig command in powershell:
PS C:\Windows\system32> ipconfig
Once I verified that there was no issue with networking. I simply created the /etc/hosts file inside my actual container from where I wanted to access docker host.
After adding this entry I can access host.docker.internal from my container. Not that I added this entry only for testing purpose. As I had to add this inside the running container. So once the container is stopped this file will be gone and you will need to create that again which is not right. So I will instead use the staic IP (10.25.241.37) of my machine instead of host.docker.internal to make request to services hosted on host machine.