Skip to content

Instantly share code, notes, and snippets.

@0x1b-xyz
Last active February 22, 2016 18:24
Show Gist options
  • Save 0x1b-xyz/d9b1cb920f3fd3bec7eb to your computer and use it in GitHub Desktop.
Save 0x1b-xyz/d9b1cb920f3fd3bec7eb to your computer and use it in GitHub Desktop.
# Call with a machine name to load the environment for that machine. Called
# without a machine name it will load a ~/.docker_env file into the shell
# environment and into launchctl
#
# Added a new var to the mix - DOCKER_HOST_IP
function useDockerMachine() {
if [ $# -eq 0 ]; then
if [ ! -f ~/.docker_env ]; then
>&2 printf "Error: No ~/.docker_env file exists. Specify machine to use.\n"
return
fi
else
docker-machine env $1 > ~/.docker_env
echo "export DOCKER_HOST_IP=\"$(docker-machine ip $1)\"" >> ~/.docker_env
fi
source ~/.docker_env
for line in `set | grep '^DOCKER_.*'`; do
pair=(${line//=/ })
launchctl setenv ${pair[0]} "${pair[1]}"
done
}
# When called with no argument, will remove all "docker-port-udp|tcp" rules found
# on the virtualbox matching DOCKER_MACHINE_NAME.
#
# For any other argument values we'll create a tcp/udp forwarding rule pair.
#
function useDockerPorts() {
if [ $# -eq 0 ]; then
RULES=$(VBoxManage showvminfo "${DOCKER_MACHINE_NAME}" | grep 'docker-port' | awk -F' ' '{print $6}' | sed -e 's/,//g')
for RULE in ${RULES[@]}; do
echo "Removing port forwarding rule ${RULE} from ${DOCKER_MACHINE_NAME} ..."
VBoxManage modifyvm "${DOCKER_MACHINE_NAME}" --natpf1 delete "${RULE}"
done
else
PORTS=( "$@" )
for PORT in ${PORTS[@]}; do
echo "Creating port forwarding rule for ${PORT} on ${DOCKER_MACHINE_NAME} ..."
VBoxManage modifyvm "${DOCKER_MACHINE_NAME}" --natpf1 "docker-port-tcp${PORT},tcp,,${PORT},,${PORT}"
VBoxManage modifyvm "${DOCKER_MACHINE_NAME}" --natpf1 "docker-port-udp${PORT},udp,,${PORT},,${PORT}"
done
fi
}
useDockerMachine
useDockerMachine default
useDockerPorts
useDockerPorts 8080
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment