Share Vagrant docker to host

1

I am setting up a Vagrant box with a development environment. I have some web apps running, some more services, a database and a docker inside Vagrant. Most services are used internally and I do port forwarding for web apps which I can access in my host machine with localhost:FORWARDED_PORT.

But one of these web apps needs some JavaScript running in docker. So it needs to access it from my web browser in the host machine. Internally, I can see this:

4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff
    inet 172.17.42.1/16 scope global docker0
       valid_lft forever preferred_lft forever

The service running in Docker is providing some static JS on http://172.17.0.2:4567. I need to be able to access that internal address from the web app in the host machine, how can I set that up?

Fernando Briano

Posted 2014-10-14T12:14:51.723

Reputation: 448

Answers

1

You don't see the internal do docker IP from outside the vagrant box VM. But you can expose in the container the port of the application (so it will be a port that can be forwarded as belonging to the vagrant VM) and refer to that JavaScript as the external IP/name if the vagrant box (even if using the SERVER_NAME env var in the page that puts the link) and the corresponding port.

gmuslera

Posted 2014-10-14T12:14:51.723

Reputation:

This doesn't seem to work. Even if I set the port on the docker run command, the js is still being served on the :4567 ip. If I curl the js file from vagrant ssh, it is working. So what I would need to do is make http://172.17.0.2:4567 from the guest machine to be available on the host machine (preferrably with the same ip/port configuration). I tried adding the guest_ip and host_ip parameters to a port forwarding config on the Vagrantfile but it doesn't work either. Is there a way to accomplish that via port forwarding?

– Fernando Briano – 2014-10-14T18:16:03.823

There are two players there, vagrant (that may have a valid external ip or a NATed one with forwarded ports), and docker, that by default have its own internal network but can put ports as they were from the host, and you should forward the port at the two levels. An alternative would be using --net=host in docker, that would use the same network as the host, so that container will be listening at the vagrant box ip for that port. In none of this cases you would be listening at the internal docker ip, for a single port is just not needed. But whatever calls it should be at the external IP. – None – 2014-10-14T18:33:51.600

1

I am doing excatly this type of thing during my everyday work. There are two things to be taken into consideration here:

  1. The port of the dockerized service has to forwarded to your vagrant vm
  2. The (now pointing to docker container) port on the vagrant box has to be forwarded to your physical box

Because I do not know your dockerized service, I take the standard distribution of dockerized Redis as an example here. As I am working with fig (http://www.fig.sh/) to start my docker containers, I will have to show you the answer to the first part in an example fig.yml:

redis:
  image: redis:latest
  ports:
   - "6379:6379"

This runs the Redis Key-Value Store in a docker container and forwards it's port 6379 to port 6379 on the vagrant box. You can now connect to Redis via localhost:6379 from INSIDE vagrant vm, so we are halfway there.

Second step is, to forward this to your local machine, by adding this to your Vagrantfile:

config.vm.network :forwarded_port, guest: 6379, host: 6379

Voila: Now you can access Redis via localhost:6379 from your physical machine.

For your case, this means that you will have to change http://172.17.0.2:4567 to http://127.0.0.1:4567 in your webapp's code, so that it actually uses the freshly setup port forwarding.

Peter Hommel

Posted 2014-10-14T12:14:51.723

Reputation: 111