Search code examples
linuxmacosdockerboot2docker

Multiple docker containers, IP addresses, VM, OSX


I am running docker on OSX via boot2docker. I am using docker remotely, via the API.

  • I create several images of a web server. Docker assigns different IP address to each container, like 172.17.0.61. Each web server is running on port 8080.

  • Inside VM, I can ping the server on this address.

How can I map these different container IP addresses (from VM) to the same one in VM, but on different port? E.G.

<local.ip>:9001 -> 172.17.0.61:8080
<local.ip>:9002 -> 172.17.0.62:8080

where local.ip may be either ip from boot2docker or anything else.

Possible solution is to define port bindings when creating container and bind each container to a different port. However, I would like to avoid that, since this config becomes part of the container, and only exist because running on OSX. If I do all this above on linux, we would not have this issue.

How to map inner containers to different ports?


Solution

  • Publishing ports is the right solution. You have the same problem whether you're running remotely or locally, just the IP address changes.

    For example, say I start the following web servers:

    $ docker run -d -p 8000:80 nginx
    $ docker run -d -p 8001:80 nginx
    

    From inside the VM (run boot2docker ssh), I can then run curl localhost:8000 or curl localhost:8001 to reach the website. This is the normal way of working with Docker on Linux. From the Mac command line, it becomes curl $(boot2docker ip):8000 because of the VM, but we've not done anything different with regards to starting the web servers because of boot2docker.