Last active
March 9, 2016 08:34
-
-
Save antho1404/e3aed12ace6dd398b318 to your computer and use it in GitHub Desktop.
Script to start a docker machine and add associated (sub)domains to the host file
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
# Script to start a docker machine and add associated (sub)domains to the host file | |
# arguments: | |
# - name of the docker machine | |
# - list of subdomains separated by a comma | |
# | |
# example | |
# start_machine myproject www,api | |
# will result to start the machine myproject and create myproject.dev, www.myproject.dev and api.myproject.dev to access the machine IP | |
function start_machine { | |
started=false | |
for machine in $(docker-machine ls | grep "virtualbox.*Running" | cut -d ' ' -f 1) | |
do | |
if [ "$machine" != "$1" ] | |
then | |
docker-machine stop $machine | |
else | |
started=true | |
fi | |
done | |
if [ $started = false ] | |
then | |
echo "Regenerate certs of $1" | |
docker-machine regenerate-certs $1 | |
echo "Starting machine $1" | |
docker-machine start $1 | |
fi | |
eval $(docker-machine env $1) | |
powder stop | |
hostfile=/etc/hosts | |
tld=dev | |
sudo sed -i.bak '/# Docker/,/# end Docker/d' $hostfile | |
ip=$(echo $DOCKER_HOST | sed "s/tcp:\/\///" | sed "s/:[0-9]*//") | |
echo "Adding domains to $DOCKER_MACHINE_NAME machine ($ip)" | |
echo "# Docker" | sudo tee -a $hostfile > /dev/null | |
echo "$ip\t$1.$tld" | sudo tee -a $hostfile > /dev/null | |
echo "$ip\twww.$1.$tld" | sudo tee -a $hostfile > /dev/null | |
echo " - $1.$tld" | |
echo " - www.$1.$tld" | |
for domain in $(echo $2 | tr ',' '\n') | |
do | |
echo "$ip\t$domain.$1.$tld" | sudo tee -a $hostfile > /dev/null | |
echo " - $domain.$1.$tld" | |
done | |
echo "# end Docker" | sudo tee -a $hostfile > /dev/null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment