Skip to content

Instantly share code, notes, and snippets.

@cookandy
Last active August 10, 2020 20:14
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cookandy/f14fadd31721dc6e136e to your computer and use it in GitHub Desktop.
Save cookandy/f14fadd31721dc6e136e to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
list=( $(docker-machine ls | grep Running | awk '{ print $1 }') )
for i in "${list[@]}"; do
ip=$(docker-machine ip $i)
#remove line matching ip
sudo sed -i '' '/'$ip'/d' /etc/hosts
#insert ip/host on last line
sudo sed -i -e '$a\
'"$ip"' '"$i"'' /etc/hosts
done
@brandonsimpson
Copy link

FYI this also removes any other lines in the /etc/hosts file that were pointing to the same ip for any other host names other than the docker machine's name.

@cookandy
Copy link
Author

Yes, you are correct. Some additional logic could be added to search for host+ip.

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