#!/bin/bash # # docker-osx # ========== # # Easy installation of Docker on OS X # https://github.com/noplay/docker-osx # # Copyright 2013 Julien Duponchelle # # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this # file except in compliance with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software distributed under # the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, # either express or implied. See the License for the specific language governing permissions # and limitations under the License. set -e export VAGRANT_CWD="$HOME/.docker-osx" DOCKER_IP="172.16.42.43" DOCKER_DOMAIN="localdocker" DOCKER_PORT="4243" DOCKER_VERSION="0.10.0" DOCKER_CLIENT_URL="http://get.docker.io/builds/Darwin/x86_64/docker-0.10.0.tgz" VAGRANT_BOX_URL="http://static.orchardup.com/binaries/vagrant/vagrant-docker-0.8.0-virtualbox.box" DOCKER_BIN="/usr/local/bin/docker" DOCKER_DEFAULTS_FILE="$VAGRANT_CWD/defaults" if [ -f "$DOCKER_DEFAULTS_FILE" ]; then . "$DOCKER_DEFAULTS_FILE" fi export DOCKER_HOST="tcp://$DOCKER_IP:$DOCKER_PORT" ########## function setup_etc_host { if [ -f "$VAGRANT_CWD/.localdocker-host" ] && [ `cat "$VAGRANT_CWD/.localdocker-host"` = "0" ] then return fi if ! grep -q localdocker /etc/hosts then echo "Adding localdocker to /etc/hosts (may need your password for sudo)..." echo "If you want you can add it manually by adding:" echo "$DOCKER_IP localdocker add the end of the /etc/hosts" echo "" echo "Or you can just ignore it and directly use the ip: $DOCKER_IP" read -p "Add the localdocker host to system configuration? [y/n] " -n 1 -r echo # move to a new line if [[ $REPLY =~ ^[Yy]$ ]] then sudo sh -c "echo '$DOCKER_IP $DOCKER_DOMAIN' >> /etc/hosts" echo -n 1 > "$VAGRANT_CWD/.localdocker-host" # We need to check the /etc/hosts at each launch else echo -n 0 > "$VAGRANT_CWD/.localdocker-host" # We never check the /etc/hosts at launch fi fi } function help() { echo "docker-osx commands:" echo " start Start local docker virtual machine" echo " halt Halt docker daemon and virtual machine" echo " suspend Suspend local docker virtual machine" echo " status Outputs status of the local docker virtual machine" echo " destroy Destroy local docker virtual machine" echo " clear Remove all docker images and (running) containers" echo " vagrant Issue subcommands directly to the vagrant CLI" echo " ssh Open SSH console on vagrant box" echo " env Outputs environment variables for Docker to connect remotely" echo " shell Open a shell with Docker VM started and environment set" echo "" } function start_vm() { # Start virtual machine if it isn't running if ! vagrant status | grep -q running then if [[ $1 == "halt" ]] then echo "Docker is not running. Exiting" exit 0 fi echo "Start Docker Virtual machine" vagrant up vagrant ssh -c "sudo stop docker; sudo start docker" fi # Update Docker on virtual machine if it needs updating if [[ $INSTALLED_DOCKER_VERSION != $DOCKER_VERSION ]] then vagrant provision vagrant ssh -c "sudo stop docker; sudo start docker" fi } ############ # Determine currently installed version of Docker INSTALLED_DOCKER_VERSION="" if [ -f $VAGRANT_CWD/.docker-version ] then INSTALLED_DOCKER_VERSION=`cat $VAGRANT_CWD/.docker-version` fi # Check that dependencies are installed if ! hash vagrant 2>/dev/null then echo "You need VirtualBox and Vagrant to use docker-osx." echo echo "You can download VirtualBox from:" echo "https://www.virtualbox.org/wiki/Downloads" echo echo "You can download Vagrant from:" echo "http://www.vagrantup.com/downloads.html" exit 1 fi # Set up Vagrant directory if [ ! -d "$VAGRANT_CWD" ] then mkdir "$VAGRANT_CWD" fi # Will want to update when docker version is bumped if [[ $INSTALLED_DOCKER_VERSION != $DOCKER_VERSION ]] then # --force-confdef to not ask about overwriting /etc/default/docker INSTALL_UPDATE_DOCKER="apt-get update; apt-get install -y -o Dpkg::Options::='--force-confdef' -o Dpkg::Options::='--force-confold' lxc-docker-$DOCKER_VERSION" fi cat > "$VAGRANT_CWD/Vagrantfile" < "$DOCKER_IP" config.vm.provision :shell, :inline => "$INSTALL_UPDATE_DOCKER" config.vm.provision :shell, :inline => "echo 'export DOCKER_OPTS=\"-H unix:///var/run/docker.sock -H tcp://0.0.0.0:$DOCKER_PORT\"' >> /etc/default/docker" config.vm.synced_folder "$(echo ~)", "$(echo ~)", :create => true config.vm.provider :virtualbox do |vb| vb.customize ['modifyvm', :id, '--memory', ENV['VM_MEMORY'] || 1024] vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on'] vb.customize ['modifyvm', :id, '--natdnsproxy1', 'on'] end vagrantfile_extra = "#{ENV['VAGRANT_CWD']}/Vagrantfile_extra.rb" eval File.open(vagrantfile_extra).read if File.exists?(vagrantfile_extra) end EOL setup_etc_host # Download Docker client if it doesn't exist or needs updating if [[ ! -f "$DOCKER_BIN" || $INSTALLED_DOCKER_VERSION != $DOCKER_VERSION ]] then echo "Installing Docker $DOCKER_VERSION client..." destdir=`dirname "$DOCKER_BIN"` destname=`basename "$DOCKER_BIN"` mkdir -p "$destdir" curl "$DOCKER_CLIENT_URL" > /tmp/docker-osx-client.tgz tar -xzf /tmp/docker-osx-client.tgz -C "$destdir" -s /docker/"$destname"/ --strip-components 3 usr/local/bin/docker chmod +x "$DOCKER_BIN" fi # Set current installed Docker version echo $DOCKER_VERSION > $VAGRANT_CWD/.docker-version case "$1" in up|start) start_vm echo echo "The virtual machine with the docker daemon is now running." echo "When you are finished and want to shut it down, just run:" echo echo " docker-osx halt" echo echo "To use docker, point the DOCKER_HOST environment variable" echo "to the docker daemon running on the virtual machine:" echo echo " eval \`docker-osx env\`" echo echo "Put this in your .profile to make it permanent." #echo "(Alternatively, pass -H=$DOCKER_HOST on the command line.)" echo echo "Then, just use the docker command from OS X directly." echo exit 0 ;; destroy) shift exec vagrant destroy "$@" ;; clear) docker kill $(docker ps -q) 2> /dev/null docker rm $(docker ps -a -q) 2> /dev/null docker rmi $(docker images -q) 2> /dev/null ;; ssh) start_vm shift exec vagrant ssh "$@" ;; stop|halt) shift exec vagrant halt "$@" ;; help) help exit 0 ;; "") help exit 0 ;; shell) echo "Starting local Docker environment..." start_vm $SHELL echo "Stopping local Docker environment..." exec vagrant halt ;; env) echo "export DOCKER_HOST=$DOCKER_HOST" exit 0 ;; vagrant) shift exec vagrant $* ;; status) exec vagrant status ;; suspend|pause) exec vagrant suspend ;; *) echo "Unrecognised command: $1" echo help exit 1 ;; esac