Skip to content

Instantly share code, notes, and snippets.

@zoomix
Last active August 29, 2015 14:08
Show Gist options
  • Save zoomix/059d8370ffea7e3df69e to your computer and use it in GitHub Desktop.
Save zoomix/059d8370ffea7e3df69e to your computer and use it in GitHub Desktop.
Change your /etc/hosts based on your running containers hostnames
#!/bin/bash
echo Starting boot2docker if not already started
boot2docker status || boot2docker up
export the_ip=$(boot2docker ip 2>&1 | egrep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*")
export old_route_ip=$(netstat -r | grep 172.17 | egrep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*")
echo Found boot2docker ip $the_ip
if [ "$the_ip" == "" ]; then
echo Could not find the ip of boot2docker.. =/
exit
fi
echo Seting up routes. Enter sudo password
if [ "$old_route_ip" != "" ]; then
sudo route -n delete -net 172.17.0.0 $old_route_ip
fi
sudo route -n add -net 172.17.0.0 $the_ip
echo setting up hosts
cd /var/tmp
cp /etc/hosts hosts
grep -v '# boot2dockerscript' hosts > hosts_temp
mv hosts_temp hosts
for containerid in $(docker ps -q); do
echo Checking out $containerid
export domain=$(docker inspect $containerid | grep '"Domainname":' | cut -f2 -d":" | cut -f2 -d'"')
export host=$(docker inspect $containerid | grep '"Hostname":' | cut -f2 -d":" | cut -f2 -d'"')
export ip=$(docker inspect $containerid | grep 'IPAdd' | egrep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*")
if [ "$domain" != "" ]; then
echo Storing values $ip $host.$domain
echo $ip $host.$domain \# boot2dockerscript >> hosts
else
echo Storing values $ip $host
echo $ip $host \# boot2dockerscript >> hosts
fi
done
sudo mv hosts /etc/hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment