Skip to content

Instantly share code, notes, and snippets.

Embed
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

This comment has been minimized.

Copy link

BelfordZ commented Feb 2, 2016

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

@laris

This comment has been minimized.

Copy link

laris commented May 14, 2019

need some update?

brew install  --without-docker-machine --without-docker docker-compose
Usage: brew install [options] formula

Install formula.

formula is usually the name of the formula to install, but it can be specified
in several different ways.

Unless HOMEBREW_NO_INSTALL_CLEANUP is set, brew cleanup will be run for the
installed formulae or, every 30 days, for all formulae.

    -d, --debug                      If brewing fails, open an interactive
                                     debugging session with access to IRB or a
                                     shell inside the temporary build directory
        --env                        If std is passed, use the standard build
                                     environment instead of superenv.If super
                                     is passed, use superenv even if the formula
                                     specifies the standard build environment.
        --ignore-dependencies        An unsupported Homebrew development flag to
                                     skip installing any dependencies of any
                                     kind. If the dependencies are not already
                                     present, the formula will have issues. If
                                     you're not developing Homebrew, consider
                                     adjusting your PATH rather than using this
                                     flag.
        --only-dependencies          Install the dependencies with specified
                                     options but do not install the specified
                                     formula.
        --cc                         Attempt to compile using provided
                                     compiler. compiler should be the name
                                     of the compiler's executable, for instance
                                     gcc-7 for GCC 7. In order to use LLVM's
                                     clang, use llvm_clang. To specify the
                                     Apple-provided clang, use clang. This
                                     parameter will only accept compilers that
                                     are provided by Homebrew or bundled with
                                     macOS. Please do not file issues if you
                                     encounter errors while using this flag.
    -s, --build-from-source          Compile the specified formula from source
                                     even if a bottle is provided. Dependencies
                                     will still be installed from bottles if
                                     they are available.
        --force-bottle               Install from a bottle if it exists for the
                                     current or newest version of macOS, even if
                                     it would not normally be used for
                                     installation.
        --include-test               Install testing dependencies required to
                                     run brew test.
        --devel                      If formula defines it, install the
                                     development version.
        --HEAD                       If formula defines it, install the HEAD
                                     version, aka. master, trunk, unstable.
        --fetch-HEAD                 Fetch the upstream repository to detect if
                                     the HEAD installation of the formula is
                                     outdated. Otherwise, the repository's HEAD
                                     will be checked for updates when a new
                                     stable or development version has been
                                     released.
        --keep-tmp                   Don't delete the temporary files created
                                     during installation.
        --build-bottle               Prepare the formula for eventual bottling
                                     during installation.
        --bottle-arch                Optimise bottles for the given architecture
                                     rather than the oldest architecture
                                     supported by the version of macOS the
                                     bottles are built on.
    -f, --force                      Install without checking for previously
                                     installed keg-only or non-migrated
                                     versions.
    -v, --verbose                    Print the verification and postinstall
                                     steps.
        --display-times              Print install times for each formula at the
                                     end of the run.
    -i, --interactive                Download and patch formula, then open a
                                     shell. This allows the user to run
                                     ./configure --help and otherwise
                                     determine how to turn the software package
                                     into a Homebrew package.
    -g, --git                        Create a Git repository, useful for
                                     creating patches to the software.
    -h, --help                       Show this message.
**Error: invalid option: --without-docker-machine**
@laris

This comment has been minimized.

Copy link

laris commented May 14, 2019

https://apple.stackexchange.com/a/352928
As of 2 February 2019, Homebrew no longer supports options link.

@abjugard

This comment has been minimized.

Copy link

abjugard commented Nov 3, 2019

Furthermore both docker-machine and docker-machine-driver-xhyve exist in brew these days, so no need to build them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.