Docker on Windows: how to connect to container from host using container IP?

31k views Asked by At

I have

  • Windows 10
  • Docker for Windows V. 1.12.5 Rev. 9503, which does not rely on boot2docker or VirtualBox anymore.

I have a number of JBoss instances running in Docker images (172.18.0.2 is a database):

  • instance 1: name: jboss-eap, IP: 172.18.0.3
  • instance 2: name: jboss-eap-arquillian, IP: 172.18.0.4

that shall be running at the same time. Each JBoss instance exports e.g. its 8787, 8080 and 8443 ports.

I have also created a bridged network: docker network create --driver bridge --subnet 172.18.0.0/24 bridged_network

Currently, I have set up a local port forwarding such that the host can access the various services using a prefix (e.g. when accessing instance 1's port 8080, the host uses localhost:28080 to connect). But that's quite error prone.

Now, I want to access those ports from the host using the container's IP, e.g. calling 172.18.0.4:8080 (next step: using the host name: jboss-eap-arquillian:8080). While this is working smoothly from container to container, I haven't been able to set it up to connect from the host.

There is Windows network interface (type: DockerNAT), having IP 10.0.75.1, but it's possible that I've created it manually (unsure ... been trying for quite some time now). But that is helpful when binding the container's port to this device, e.g. docker run ... -p 10.0.75.1:8080:8080. I can then call the service using 10.0.75.1:8080, but that does not help me for the 2nd container.

I've also tried to use the Docker's host network device, but that a. seems to work for only one machine b. I cannot statically set the IP, which is needed for JUnit tests running a static configuration. Note: I cannot rely on the network DHCP to assign an IP upon startup, since I change my network (and therefore the DHCP) frequently, resulting in an unfixed IP again.

So basically I'm looking for a way to set up the network/container in such a way, that I can call the service provided by the container (e.g. on port 8080) using the container IP (e.g. 172.18.0.3 for instance 1) from the host using 172.18.0.3:8080.

2

There are 2 answers

3
Derick Bailey On

you can't. docker is not a virtual machine, and you don't get access to the docker host via IP address.

see my same question here: https://forums.docker.com/t/access-dockerized-services-via-the-containers-ip-address/21151

and my realization of how this works, here: https://derickbailey.com/2016/08/29/so-youre-saying-docker-isnt-a-virtual-machine/

if you need to use the app hosted in the container, from your localhost, expose the port of the app with the -p option of docker run

docker run -p 8080:8080 image_name

and then connect to localhost:8080 for that service

1
hrakup On

On docker for windows you can use address 10.0.75.1:8080 but you need to configure your firewall, a better way is using address 10.0.75.2:8080,

for both addresses you have to publish your port when you run container

docker run -p 8080:8080 image_name

More info https://github.com/docker/for-win/issues/334#issuecomment-297030101