Skip to content

Instantly share code, notes, and snippets.

@ianlintner-wf
Last active October 29, 2019 20:16
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ianlintner-wf/a2b917158aaf100d9fb3 to your computer and use it in GitHub Desktop.
Save ianlintner-wf/a2b917158aaf100d9fb3 to your computer and use it in GitHub Desktop.
A basic shell script to use a configuration file to update local development hosts ip running on docker-machine (virtual box) and Mac OSX
#!/usr/bin/env bash
#Usage docker-machine-update-hosts.sh hosts.conf default
#arg1 is a configuration file with hosts
#arg2 is the docker-machine name e.g. default
DOCKER_IP=$(docker-machine ip $2)
echo "$2 ip: $DOCKER_IP"
#Remove existing lines from hosts
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "Removing existing domain $line"
sudo sed -i '' '/'$line'/d' /etc/hosts
done < "$1"
#Add new hosts to the bottom of the file
while IFS='' read -r line || [[ -n "$line" ]]; do
echo "Adding entry $DOCKER_IP $line"
sudo echo "$DOCKER_IP $line" >>/etc/hosts
done < "$1"
example.dev
other-site.local
my-site.com
@ianlintner-wf
Copy link
Author

This could also be array based with out the need for a separate hosts.conf file, but this is more portable.

@jalexanderfox
Copy link

you could do:

machine_name=${2:-$(docker-machine active)}
DOCKER_IP="$(docker-machine ip $machine_name)"

if you wanted to default the second parameter to the "active" docker-machine name.

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