Skip to content

Instantly share code, notes, and snippets.

@jgsqware
Created November 10, 2015 13:11
Show Gist options
  • Save jgsqware/9a208ca5cf6590b25150 to your computer and use it in GitHub Desktop.
Save jgsqware/9a208ca5cf6590b25150 to your computer and use it in GitHub Desktop.
Docker: Update your hosts with docker-machine ips automatically
# Pattern
# <docker-machine name>=<host name>,<host name>,...
registry=docker-registry
#!/usr/bin/env bash
#Usage docker-machine-update-hosts.sh hosts.conf
#arg1 is a configuration file with hosts
#Remove existing lines from hosts
while read -r line; do
[[ "$line" =~ ^#.*$ ]] && continue
echo "Configuration: $line"
IFS='=' read -a configuration <<< "$line"
machine=${configuration[0]}
IFS=',' read -a hosts <<< "${configuration[1]}"
DOCKER_IP=$(docker-machine ip $machine)
for host in "${hosts[@]}"; do
echo "Adding $DOCKER_IP $host"
sudo bash -c "sed -i '' '/^192\.168\.99\.[[:digit:]]\{0,3\}[[:space:]]*$host/d' /etc/hosts"
sudo bash -c "echo '$DOCKER_IP $host' >> /etc/hosts"
done
done < "$1"
@jgsqware
Copy link
Author

Run it with

> update-hosts-docker-machine.sh hosts.conf

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