0

I am trying to build an application that can control dockers containers hosted on Docker for windows.

The application is built on top of docker-java library and it works fine.

I tried to host this application on docker itself. Hosting was OK. The issue is that, when the application tries to connect to tcp://localhost:2375 in order to access docker's api, the connection fails.

This is obviously because localhost within the container is not anymore refering to the actual host where Docker for Windows is installed.

So I used the explicit IP address to access docker's api from the container (tcp://192.168.0.10:2375), the connection was also refused!

I stopped the firewall, yet the issue was still there.

I searched for it, then I realized that I have to define "hosts" configuration variable to make docker's api accessable using the host's explicit IP address. But when I tried to set the "hosts" configuration variable, docker said: "hosts": Cannot be used in Docker for Windows

So does it mean that there is no way on Docker for Windows to host a container that can access its api?

mksoi
  • 131
  • 3
  • 13

1 Answers1

0

Though I am not quite familiar with Docker on Windows, I found some introductions on docker website. Just try below.

I WANT TO CONNECT FROM A CONTAINER TO A SERVICE ON HOST.

But I strongly suggest you give Docker on Windows up.

Edited: 2018-9-5 19:55
Using host.docker.internal to access the host as comment suggested below.

Light.G
  • 5,548
  • 1
  • 14
  • 25
  • Thank you for your reply. I used **host.docker.internal** name to access the docker API from the container and it works. Surprisingly, the IP address of **host.docker.internal** is **192.168.65.2** even though **192.168.65.0** network is not defined in the host at all! **DockerNAT** is defined as **10.0.75.0** network. It seems that there is a bridge defined within docker, from the containers' side it uses **192.168.65.0** network and from the host's side it uses **10.0.75.0** when **--net=host** option is used. Containers that are not attached to the host network, use **172.17.0.0**. – mksoi Sep 05 '18 at 11:26
  • But still there is an issue. I can not access the container's service from the network. Containers hosted normally (without **--net=host** option) on 172.17.0.0 network is accessible from the network using the host's IP address and there exposed/mapped port numbers. However, the container hosted using (**--net=host** option) is not accessible from the network using the host's IP address! I can not use the option **-p 5000:8080** along with **--net=host** option as docker refused this command because the whole container is attached to the host network. – mksoi Sep 05 '18 at 12:00
  • See the new comment plz @Light.G – mksoi Sep 05 '18 at 12:10
  • @mksoi Aye, **—net=host** cannot work with **-p**. Now, you mean container can access to host, but not the opposite? That doesn’t make sense. – Light.G Sep 05 '18 at 12:28
  • Container X (hosed with **--net=host** option) can access host using name **host.docker.internal** which is resolved to **192.168.65.2** (an unknown network to the host as explained in the 1st comment). Host can access container X using **DockerNAT** network and IP= **10.0.75.2**. However, any other device in the host's LAN network, can access containers that are not hosted using **--net=host** option through the host's LAN IP address= **145.118.70.40**, however, they can not access container X (hosted with **--net=host** option) in the same way!!! – mksoi Sep 05 '18 at 12:37
  • For more details, consider container Y hosted by cmd: **docker run -it -p 7000:8080 webserver**. I can access its service using: **http:// 145.118.70.40:7000** from any device on the host's network. For container X hosted by cmd: **docker run -it --net=host webserver**, the host can access its service via **http:// 10.0.75.2:8080**, however, other devices on the network can not use **http:// 145.118.70.40:8080** to access its service. Where: **webserver** is a Tomcat image that opens port 8080, the **Dockerfile** expose 8080 for both containers, and the host's IP address is: **145.118.70.40** – mksoi Sep 05 '18 at 12:52
  • @mksoi Weird... Got confused by your serval IPs. Never met on Linux. – Light.G Sep 05 '18 at 15:29