Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind the VM docker-machine creates to OSX IP address?

I'm developing locally with Docker on OSX using the latest Docker toolkit. I have a node server running in a docker container, bound to port 9999 of the VM. I can hit this server from a browser on my mac, and I would like to hit from another device on the same network. Is there a way to bind the VM to the machine's IP address? Or otherwise expose it?

like image 912
geddski Avatar asked Dec 18 '22 23:12

geddski


2 Answers

I just figured this one out today! I am using docker-machine with virtualbox on Mac OS 10.10.5. The first thing I tried was to change the network interface from NAT to Bridged. This just breaks docker-machine's ability to communicate with the VM.

Instead I ADDED another network adapter running in bridged mode. After starting the docker-machine I get this:

$ docker-machine ip redis-test
10.222.11.242

That is a local network address accessible from anyone else in my office or on my VPN.

Then if I run something like:

$ docker run -p 6379:6379 -d redis

I get a containerized redis service running on port 6379 of the 10.222.11.242 address.

So I can do this from anywhere else on the network:

$ telnet 10.222.11.242 6379
Trying 10.222.11.242...
Connected to 10.222.11.242.
Escape character is '^]'.
info
$1827
# Server
redis_version:2.8.19
...

And as a cool bonus of this we can remap the ports like this:

$ docker run -p 8080:6379 -d redis
e7cc53d9c157a658041c3bee5967dd3678b4d35e6146a02220a87bfebfc919ad
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
e7cc53d9c157        redis               "/entrypoint.sh redis"   7 seconds ago       Up 6 seconds        0.0.0.0:8080->6379/tcp             goofy_yonath
bf1dc6c7c6b5        redis               "/entrypoint.sh redis"   51 minutes ago      Up 51 minutes       0.0.0.0:6379->6379/tcp             redis

Now I have two redis instances listening on different ports (6379 and 8080) of the same IP.

EDIT: Here are some details to help those confused about adding a NIC to the VM in VirtualBox. I have only used VirtualBox for this and cannot advise about other virtualization system configurations.

  1. Stop the VM by selecting it in the VM Manager and using the right-click menu or pressing 'command-F'.
  2. Click "Settings".
  3. Click "Network".
  4. Select one of the Adapters that is not currently enabled.
  5. Enable it.
  6. Select "Bridged Adapter" in the "Attached to" selection.
  7. Click OK.
  8. Start your VM and try it out.

NOTE: I am sure there are some clever command line options for doing this setup, but since I only ever needed to set it once I have never bothered to automate it.

like image 96
e.thompsy Avatar answered Mar 08 '23 01:03

e.thompsy


You could setup port forwarding on the VirtualBox NAT adaptor.

Bridging it to the local network (in the answer above) is not the same as using the OSX IP address. Bridging can sometimes cause extra headaches if you are on laptop and move to different internet connections. The VM may not automatically pull a new IP from the new network, etc.

In the UI go to Settings --> Network --> Port Forwarding or from the commandline something like this:

VBoxManage controlvm "default" natpf1 "tcp-port9999,tcp,,9999,,9999";

where "default" is the name of the VM ("default" is normally used for docker-machine) and 9999 is the port you want to map.

More info at: https://github.com/boot2docker/boot2docker/blob/master/doc/WORKAROUNDS.md

https://www.virtualbox.org/manual/ch06.html (Configuring Port forwarding with NAT)

like image 23
Kristofor Carle Avatar answered Mar 08 '23 01:03

Kristofor Carle