Skip to content

Instantly share code, notes, and snippets.

@wojtekmach
Forked from mmerickel/README.rst
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wojtekmach/0d0d0faeeb6ec4d45ef0 to your computer and use it in GitHub Desktop.
Save wojtekmach/0d0d0faeeb6ec4d45ef0 to your computer and use it in GitHub Desktop.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment