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

15,006

Solution 1

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>

Solution 2

I just wrote a tutorial of how to use a host-only network and TCP routing to make this pretty easy. This way you don't have to map every specific port.

http://ispyker.blogspot.com/2014/04/accessing-docker-container-private.html

Important points ...

1) Add host-only network to Virtual Box 2) Tell the boot2docker VM to have an adapter on the host-only network 3) Add an IP for the new boot2docker VM host-only networking adapter 4) Route all Mac OS X traffic for the docker container subnet to that boot2docker VM host-only networking IP

Actual steps are on the blog with output so you can compare to what you see as you follow them.

Solution 3

I have installed tomcat from my Dockerfile and forwarded that to 6060 using vagrant`s port forwarding. These are the steps worked for me:

vagrant provision
vagrant up
vagrant ssh
box_name$ docker run  -i -t -p 8080:8080 bsb_tomcat6 /bin/bash

Able to see tomcat up & running on localhost:6060, as I have done port forwarding to 6060 in my Vagrantfile

Share:
15,006
Joe
Author by

Joe

Updated on June 15, 2022

Comments

  • Joe
    Joe almost 2 years

    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.
  • bojo
    bojo almost 11 years
    One can do docker run -d -p :8989 now to directly map a port to the same port publicly as of github.com/dotcloud/docker/commit/…
  • Dav Clark
    Dav Clark over 10 years
    Has there been a change in Docker that disabled this functionality? I am unable to get to my docker container from the host (OS X) without using vagrant ssh to create a tunnel.
  • creack
    creack over 10 years
    @DavClark have you set a static IP for your vagrant vm? Docker now uses unix socket by default. You need to enable the TCP mode. You can run the damon like this: docker -d -H tcp://0.0.0.0:4243 and the client like this: docker -H tcp://<ip of vm>:4243 run -i -t base bash
  • Dav Clark
    Dav Clark over 10 years
    The 0.0.0.0 is the piece I was missing. The container was just listening on localhost. The documentation is certainly not clear on this issue yet!
  • Anton K
    Anton K about 10 years
    Welcome to SO! Even though you included the URL, it's also helpful to include the important points in the answer.
  • Andrew
    Andrew about 10 years
    Added more info. Thanks for the comment.