Skip to content

Instantly share code, notes, and snippets.

@bhyde
Last active November 2, 2019 18:59
Show Gist options
  • Save bhyde/be920f1a390db5f4148e to your computer and use it in GitHub Desktop.
Save bhyde/be920f1a390db5f4148e to your computer and use it in GitHub Desktop.
Boot2docker - teach the mac how to connect to the containers by adjusting it's routing.
#!/bin/bash
# Script to instruct the Mac how to route packets to the
# software defined network where containers created via boot2docker
# reside. This lets you casually directly to ports (ssh, http, etc. etc.)
# on those containers.
function ERROR(){ echo "ERROR: $*" ; }
function FAIL(){ echo "FAILING: $*" ; exit 1; }
if [[ 'running' != $(boot2docker status) ]]
then FAIL "boot2docker's VM not running" ; fi
IP_OF_DOCKER_HOST=$(boot2docker ip 2> /dev/null)
SDN_NET_FOR_CONTAINERS=$(boot2docker ssh ip route show 2> /dev/null | awk '/docker0/{print $1}' )
echo "Establishing route to $SDN_NET_FOR_CONTAINERS via $IP_OF_DOCKER_HOST"
if netstat -rn | awk "\$2 == \"$IP_OF_DOCKER_HOST\" {exit(1);}" ; then
sudo route -n add "$SDN_NET_FOR_CONTAINERS" "$IP_OF_DOCKER_HOST"
else
ERROR "Already routing something to the docker VM ..."
netstat -rn \
| awk '$2 == "192.168.59.103"||/Destination.*Refs/{print;}'
FAIL "... so never mind."
fi
@philpraxis
Copy link

Just so you know, boot2docker VM can exist on some other IP, for example if you delete and init/run a new boot2docker, you'll get .104 :

docker@boot2docker:~$ ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 08:00:27:AA:BB:CC
          inet addr:192.168.59.104  Bcast:192.168.59.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:feaa:bbcc/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:366709 errors:0 dropped:0 overruns:0 frame:0
          TX packets:55677 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:500744316 (477.5 MiB)  TX bytes:6978246 (6.6 MiB)

@emschwartz
Copy link

Thanks a lot for posting this! Helped a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment