Created
November 10, 2015 13:11
-
-
Save jgsqware/9a208ca5cf6590b25150 to your computer and use it in GitHub Desktop.
Docker: Update your hosts with docker-machine ips automatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pattern | |
# <docker-machine name>=<host name>,<host name>,... | |
registry=docker-registry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run it with
> update-hosts-docker-machine.sh hosts.conf