Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container - how to configure so it gets a viable IP address when running in vagrant?

Docker (www.docker.io) looks terrific. However, after installing VirtualBox, Vagrant ... and finally Docker on a Mac, I'm finding it's not possible to access the service running in the Docker container from another computer (or from a terminal session on the Mac). The service I'm trying to access is Redis.

The problem appears to be that there's no route to the IP address assigned to the Docker container. In this case the container's IP is 172.16.42.2 while the Mac's IP is 196.168.0.3.

A couple notes:

  1. It IS possible to access it - but only from within the VirtualBox session. This can be done using redis-cli -h 172.16.42.2 -p 6379.
  2. I have added "config.vm.network :bridged" to the VagrantFile in an attempt to get the, but that didn't solve the problem.
like image 771
Joe Avatar asked Apr 29 '13 16:04

Joe


Video Answer


1 Answers

The VM generated by vagrant is indeed isolated, in order to access it from your host, you can allocate a private network to it. Instead of doing config.vm.network :bridged, try config.vm.network :private_network, ip: "192.168.50.4", It should do the trick

However, this will only allow you to access the VM itself, not the containers.
In order to do so, when running the container, you can add the -p option

ex: docker run -d -p 8989 base nc -lkp 8989

This will run a netcat listening on 8989 within a container and expose the port publicly. As it is also run with -d, the container will be in detached mode and the only output will be the container's ID

In order to expose the port, Docker do a simple NAT. In order to know the real port, you can

do docker port <ID of the container> 8989

Netcat will be available from the mac at 192.168.50.4:<result>

like image 133
creack Avatar answered Oct 03 '22 21:10

creack