Skip to content

Instantly share code, notes, and snippets.

@mmerickel
Last active September 13, 2017 15:54
Show Gist options
  • Save mmerickel/e213fbe7ec7728e4d043 to your computer and use it in GitHub Desktop.
Save mmerickel/e213fbe7ec7728e4d043 to your computer and use it in GitHub Desktop.
boot2docker on os x

How to setup docker on OS X using boot2docker

Basic install

  1. Install Virtualbox

  2. Install XCode and the Command Line Tools

  3. Install homebrew:

    ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
    
  4. Install boot2docker:

    brew install boot2docker
    
  5. Bootstrap boot2docker:

    boot2docker init
    boot2docker up
    
  6. Add the DOCKER_HOST to your profile:

    echo "export DOCKER_HOST=tcp://$(boot2docker ip 2> /dev/null):2375" >> ~/.bashrc
    

Setup DNS

We will name the VM localdocker, allowing us to always be able to access the running containers on whatever port they are exposed.

Update your /etc/hosts file:

echo "$(boot2docker ip 2> /dev/null) localdocker" | sudo tee --append /etc/hosts

Now when a container is run:

docker run -p 5000:80 nginx

We can connect to the container via `http://localdocker:5000/`_.

Bind mounting from OS X into docker containers

  1. Download the boot2docker iso with Virtualbox Guest Additions:

    boot2docker down
    curl http://static.dockerfiles.io/boot2docker-v1.2.0-virtualbox-guest-additions-v4.3.14.iso > ~/.boot2docker/boot2docker.iso
    
  2. Mount /Users into the VM in order to support binding volumes into docker containers:

    VBoxManage sharedfolder add boot2docker-vm -name home -hostpath /Users
    
  3. Start boot2docker:

    boot2docker up
    

Install nsenter for inspecting containers

An appropriate way to inspect a running container is via nsenter. It can drop us into a shell inside of the container's filesystem and inspect its running processes. Unfortunately it only works on linux, so we will create a docker-enter script that works for us over ssh.

  1. Build and install nsenter. You can run this from the host because the bind-mounting is still only from the VM:

    docker run --rm -v /var/lib/boot2docker:/target jpetazzo/nsenter
    
  2. Setup docker-enter script for easy inspection in OS X:

    cat > /usr/local/bin/docker-enter <<'EOF'
    #!/bin/bash
    set -e
    
    # Check for nsenter. If not found, install it
    boot2docker ssh '[ -f /var/lib/boot2docker/nsenter ] || docker run --rm -v /var/lib/boot2docker/:/target jpetazzo/nsenter'
    
    # Use bash if no command is specified
    args=$@
    if [[ $# = 1 ]]; then
        args+=(/bin/bash)
    fi
    
    boot2docker ssh -t sudo /var/lib/boot2docker/docker-enter "${args[@]}"
    EOF
    
    chmod +x /usr/local/bin/docker-enter
    
@gambl
Copy link

gambl commented Feb 9, 2015

Great info.

For BSD tee (by default on OS X) the command in "Setup DNS" should use -a for append (not --append) i.e:

echo "$(boot2docker ip 2> /dev/null) localdocker" | sudo tee -a /etc/hosts

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