Skip to content

Instantly share code, notes, and snippets.

@alexw23
Last active December 2, 2015 04:18
Show Gist options
  • Save alexw23/6eb66ebcaad3bd0182f8 to your computer and use it in GitHub Desktop.
Save alexw23/6eb66ebcaad3bd0182f8 to your computer and use it in GitHub Desktop.
Automatically add docker machines to /etc/hosts
#this is a short-hand function to load a docker env
docker-env() {
eval "$(docker-machine env ${1:-default})"
}
#add this function to your .bash_profile
update-docker-hosts(){
# clear existing *.docker.local entries from /etc/hosts
sudo sed -i '' '/\.docker.local$/d' /etc/hosts
# iterate over each machine
docker-machine ls | tail -n +2 | awk '{print $1}' \
| while read -r MACHINE; do
MACHINE_IP="$(docker-machine ip ${MACHINE} 2>/dev/null)"
[[ -n $MACHINE_IP ]] && sudo /bin/bash -c "echo \"${MACHINE_IP} ${MACHINE}.docker.local\" >> /etc/hosts"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment