What IP do I access when using docker and boot2docker?

[This post explores troubleshooting network issues you might find when using docker with boot2docker, and in doing so illustrates the network topology of services running inside of docker and a VM host like VirtualBox]

Docker is an amazing project, offering to remove the burden of managing servers when you want to play with a technology stack, and offering to remove the burden of deploying those stacks into production when you are ready to launch your new venture into the world. I love the promise of Docker because it allows me to easily handle installation of an entire technology stack (using the docker registry which is a store of docker images, bundles of software components that others have configured and published for use by anyone) and then quickly deploy that exact configuration onto scalable Iaas providers like Digital Ocean or AWS. Instead of trying to figure out how to install a new mysterious Java service like Mesos and make sure I have all the necessary components and configured (a real painful issue unless you are a Java expert) I can run command like this:

    $ docker search mesos
    NAME                                   DESCRIPTION    STARS     OFFICIAL   AUTOMATED
    redjack/mesos-master                                  3                    [OK]
    $ docker run -P redjack/mesos-master

And, docker will pull a preconfigured image with all the necessary components, and run it inside a docker container. Then, you can run a command docker ps to retrieve the “exposed” port and access the Mesos control panel on your laptop.

    $ docker ps
    CONTAINER ID     ....     STATUS             PORTS                    NAMES
    fcd04b12ed24     ....     Up 42 seconds>5050/tcp   jovial_meitner 

This tells us that we can go to http://localhost:49153/ to see the Mesos control panel. Specifically, that the Docker host system has mapped the default 5050 port on which Mesos listens inside the container to a random public port 49153. The IP address is the “ANY” address, meaning you can use localhost or the IP of your Wi-Fi connection, or even itself.

However, this did not work for me. None of these addresses worked correctly.

One note of clarification: Docker runs best on Linux itself, but you can easily play with Docker on OSX using the boot2docker tool. boot2docker installs a tiny host system (running as a VM on VirtualBox) which then integrates with the docker client and hosts your containers. boot2docker also creates a mapping of IP addresses internal to the host VM and to external adapters managed by the VirtualBox host. I’m using boot2docker here, and this is why I am having these issues.

To troubleshoot this problem, let’s look deeper into what is happening with our container. We can run a command called docker inspect to view the current state of any container; provide it with the name or ID of a container retrieved using the docker ps command. This command is a shortcut to all that (which works if you are running a single docker container):

    $ docker inspect $(docker ps -q) | grep IPA
      "IPAddress": "",

This tells us that the IP that our container is using is Let’s see if we can ping this IP address from our OSX laptop.

    $ ping
    PING ( 56 data bytes
    Request timeout for icmp_seq 0
    Request timeout for icmp_seq 1

No luck! But, if we jump inside our boot2docker system and try the same command:

    $ boot2docker ssh
    Warning: Permanently added '[localhost]:2022' (RSA) to the list of
    known hosts.
                            ##        .
                      ## ## ##       ==
                   ## ## ## ##      ===
               /""""""""""""""""___/ ===
          ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
               ______ o          __/
     _                 _   ____     _            _
    | |__   ___   ___ | |_|___  __| | ___   ___| | _____ _ __
    | '_  / _  / _ | __| __) / _` |/ _  / __| |/ / _  '__|
    | |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
    |_.__/ ___/ ___/ __|_______,_|___/ ___|_|____|_|
    boot2docker: 1.0.0
                 master : 16013ee - Mon Jun  9 16:33:25 UTC 2014
    docker@boot2docker:~$ sudo su
    root@boot2docker:/home/docker# ping
    PING ( 56 data bytes
    64 bytes from seq=0 ttl=64 time=0.084 ms
    $ curl -I
    HTTP/1.1 200 OK
    Date: Thu, 12 Jun 2014 03:59:51 GMT
    Content-Length: 4614
    Content-Type: text/html
    docker@boot2docker:~$ curl -I http://localhost:49153/
    HTTP/1.1 200 OK
    Date: Thu, 12 Jun 2014 04:00:02 GMT
    Content-Length: 4614
    Content-Type: text/html    

In this case, we can ping the IP address successfully. The container is running properly and our docker host system can see the container IP, and we can see the service running off the host IP on the correct port and our host system successfully has forwarded the port to the localhost port 49153.

For some reason, our IP is not routable on our Mac laptop. boot2docker did not properly establish the routing between our VirtualBox system, our boot2docker host, and our OSX laptop. Advanced users could probably muck around and figure out what is wrong by looking into the network adapter configuration for VirtualBox, but we are using boot2docker because we wanted to ignore this minutiae. There is a simpler way, if you can tolerate your own ignorance (I’m good at this).

    $ boot2docker ip
    The VM's Host only interface IP address is:

We now know the IP address of the boot2docker host. If we use this with the port attached, we can now see Mesos running.

Whew! I’m glad this post is done. Running docker fries my battery.