Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Use native virtualization on OS X docker with xhyve

What this?

So one of the painful points of using docker on OS X is that you need to run a virtualbox VM, which often suffers from performance issues. With xhyve, a OS X virtualization system, and docker-machine-xhyve you can now have docker use the native OS X hypervisor to run containers.

No more dealing with virtualbox shenanigans!

In this script, I've also set up a way to autoconfigure terminal sessions to load docker's environment vars (dependent on docker-machine) so you do not have to run eval $(docker-machine env whatever) every time you open a new terminal window.

Requirements

  • At least OS X 10.10 (Yosemite) because Hypervisor.framework, on which xhyve depends, was first introduced in Yosemite.
  • homebrew

Before you begin

Uninstall Docker Toolbox:

$ sh -c "$(curl -fsSl https://raw.githubusercontent.com/docker/toolbox/master/osx/uninstall.sh)"

And remove existing caches

$ sudo rm -rf ~/.docker

Usage

To install:

$ sh -c "$(curl -fsSL https://gist.githubusercontent.com/0x414A/0d5303b787a449cd564f/raw/install.sh)"

To uninstall:

$ sh -c "$(curl -fsSL https://gist.githubusercontent.com/0x414A/0d5303b787a449cd564f/raw/uninstall.sh)"

Known Issues / shenanigans to be aware of

  • After rebooting, when you open a Terminal / iTerm session, there will be an error regarding TLS certs. You will need to run docker-machine restart dev.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>{{user-path}}</string>
</dict>
<key>Label</key>
<string>com.docker.machine.dev</string>
<key>ProgramArguments</key>
<array>
<string>{{docker-machine}}</string>
<string>start</string>
<string>dev</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
GIST_URL='https://gist.githubusercontent.com/0x414A/0d5303b787a449cd564f/raw'
setup_shell() {
[ -f ~/.profile ] && \
mv ~/.profile{,.backup} && \
echo "Created backup of existing ~/.profile to ~/.profile.backup"
curl $GIST_URL/profile -o ~/.profile && echo "Setup $SHELL"
if [[ $SHELL == *"zsh"* ]];
then
[ -f ~/.zprofile ] && mv ~/.zprofile{,.backup}
curl $GIST_URL/zprofile -o ~/.zprofile
fi
}
setup_docker_machine() {
## Prerequisites
echo 'Installing xhyve, docker, docker-compose, and go'
brew update
brew install xhyve docker go
brew install docker-compose --without-docker-machine --without-docker
## Set up Go
echo 'Setting up go directories'
mkdir ~/.go
## Set up shell
if [[ $SHELL == *"zsh"* ]]; then
shellrc=$HOME/.zshrc
else
shellrc=$HOME/.bash_profile
fi
echo 'export GOPATH=$HOME/.go' >> $shellrc
export GOPATH=$HOME/.go
## Install docker-machine
go get github.com/docker/machine
cd $GOPATH/src/github.com/docker/machine
make build
make install
## Install docker-machine xhyve
export GO15VENDOREXPERIMENT=1
go get -u github.com/zchee/docker-machine-driver-xhyve
cd $GOPATH/src/github.com/zchee/docker-machine-driver-xhyve
make build
make install
sudo chown root:wheel $GOPATH/bin/docker-machine-driver-xhyve
sudo chmod u+s $GOPATH/bin/docker-machine-driver-xhyve
## Create the xhyve machine
docker-machine create dev --driver xhyve --xhyve-experimental-nfs-share
mkdir -p ~/Library/LaunchAgents
curl $GIST_URL/com.docker.machine.dev.plist -o\
~/Library/LaunchAgents/com.docker.machine.dev.plist && \
echo "Setup docker-machine to launch on startup"
}
setup_docker_machine
setup_shell
echo "Now, reboot."
if [[ $(pgrep docker-machine-driver-xhyve) ]]; then
if [[ $(docker-machine status dev) == *"Running"* ]]; then
eval $(docker-machine env dev)
else
echo "It looks like docker-machine did not start correctly :("
fi
else
echo "docker-machine dev is not running"
echo "try running: docker-machine start dev"
fi
remove_shell_setup() {
rm ~/.profile
[ -f ~/.profile.backup ] && \
mv ~/.profile{.backup,} && \
echo "Restored ~/.profile.backup to ~/.profile"
if [[ $SHELL == *"zsh"* ]]
then
rm ~/.zprofile
[ -f ~/.zprofile.backup ] && mv ~/.zprofile{.backup,}
fi
}
remove_docker_machine_setup() {
rm ~/Library/LaunchAgents/com.docker.machine.dev.plist && \
echo "Removed docker-machine from startup"
}
remove_shell_setup
remove_docker_machine_setup
echo "Removed all the things."
emulate sh -c '. ~/.profile'

BelfordZ commented Feb 2, 2016

Wow great fork. Thanks man!!!!!!!!

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