Skip to content

Instantly share code, notes, and snippets.

@robinbb
Created February 2, 2016 16:15
Show Gist options
  • Save robinbb/f8689fe06e3003f0cebe to your computer and use it in GitHub Desktop.
Save robinbb/f8689fe06e3003f0cebe to your computer and use it in GitHub Desktop.
Establish network route to Docker Machine subnet
#! /bin/sh
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
if [ "$#" -ne 1 ] ; then
echo "Usage: $0 vm-name"
exit 1
fi
VM_NAME="$1"
DOCKER_MACHINE_CMD=${DOCKER_MACHINE_CMD:-docker-machine}
if [ "Running" != $($DOCKER_MACHINE_CMD status "$VM_NAME") ] ; then
echo "The docker-machine VM named '$VM_NAME' is not running." \
"Start it, first. Exiting"
exit 1
fi
DOCKER_MACHINE_IP=$($DOCKER_MACHINE_CMD ip "$VM_NAME")
netstat -rn | awk "\"$DOCKER_MACHINE_IP\" == \$2 { exit 123 }"
if [ 123 = $? ] ; then
echo "The docker machine VM already has a routing table entry. Exiting."
exit 1
else
DOCKER_VMS_CIDR=$($DOCKER_MACHINE_CMD ssh "$VM_NAME" ip route show \
| awk '/docker0/ { print $1 }')
sudo route -n add "$DOCKER_VMS_CIDR" "$DOCKER_MACHINE_IP"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment