13

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
kniffte
  • 153
  • 1
  • 2
  • 7

2 Answers2

13

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

hrakup
  • 139
  • 1
  • 4
  • This helped me, but to elaborate a bit: the `-p` flag maps ports from host to container. In the example, they're the same port. The first one is the port on the _host_, the second one is the port on the _container_. For instance, to run a Redis container on port 8083, you invoke like this: `docker run -p 8083:6379 redis`. What you're say is "Anything that comes in to my 'outer' host machine on port 8083 should be forwarded to this container on port 6379" (which is the standard port for Redis). – Deane Jun 29 '18 at 14:33
0

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

Derick Bailey
  • 72,004
  • 22
  • 206
  • 219