Docker on your Mac using Vagrant

The first time I tried docker I used boot2docker without complete knowledge of it. The idea behind boot2docker is really quite clever: since we can’t run docker natively on Mac, we install a bare-bones Linux VM that can run docker, and then we communicate with it using a docker client running on our host.

After some initial tests I started looking for a different solution considering that I already installed Vagrant on my machine.

This guide explain how to use docker on mac using Vagrant with VirtualBox or Parallels as provider without installing boot2docker.

Prerequisites:

Vagrant installation

$ brew cask install vagrant

Provider installation

Vagrant has the ability to manage different provider such as VirtualBox, Parallels and VMware. By default it uses VirtualBox which is free, other providers need license to run.

If you want to use default provider just install VirtualBox:

$ brew cask install virtualbox

Otherwise if you have already installed Parallels on your machine:

$ vagrant plugin install vagrant-parallels

The Vagrant plugin installer will automatically download and install vagrant-parallels plugin.

Docker installation

$ brew install docker

Build and run your docker VM

Create a new directory, name it docker and create Vagrantfile:

$ mkdir docker
$ cd docker
$ touch Vagrantfile

Now edit your Vagrantfile and add:

Vagrant.require_version ">= 1.6.5"

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "yungsang/boot2docker"
  config.vm.network "private_network", ip: ENV['BOOT2DOCKER_IP'] || "10.211.55.5"

  config.vm.provider "parallels" do |v, override|
    override.vm.box = "parallels/boot2docker"
    override.vm.network "private_network", type: "dhcp"
  end

  # Fix busybox/udhcpc issue
  config.vm.provision :shell do |s|
    s.inline = <<-EOT
      if ! grep -qs ^nameserver /etc/resolv.conf; then
        sudo /sbin/udhcpc
      fi
      cat /etc/resolv.conf
    EOT
  end

  # Adjust datetime after suspend and resume
  config.vm.provision :shell do |s|
    s.inline = <<-EOT
      sudo /usr/local/bin/ntpclient -s -h pool.ntp.org
      date
    EOT
  end
end

Now you’re able to run your VM:

### VirtualBox
$ vagrant up

### Parallels
$ vagrant up --provider parallels

Finally, you’ll need to set an environment variable called DOCKER_HOST that will tell your Docker client on your host machine the URI for the Docker daemon running on the VM.

$ export DOCKER_HOST="tcp://`vagrant ssh-config | sed -n "s/[ ]*HostName[ ]*//gp"`:2375"

Once you’ve completed all these steps, you should have the Docker client installed and a VM capable of starting Docker containers. To test it out, try the command docker run hello-world from your host:

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from hello-world
a8219747be10: Pull complete
91c95931e552: Already exists
hello-world:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:aa03e5d0d5553b4c3473e89c8619cf79df368babd18681cf5daeb82aab55838d
Status: Downloaded newer image for hello-world:latest
Hello from Docker.
...

Good Dockering! 😉

Tip

In some case you can encounter this error “Error response from daemon: client and server don’t have same version”. To solve this problem install older or newer Docker version on your machine.

$ brew tap homebrew/versions
$ brew install homebrew/versions/docker
$ brew search docker
docker
homebrew/versions/docker133
homebrew/versions/docker141
homebrew/versions/docker150
$ brew install homebrew/versions/docker150

Leave a Reply

wpDiscuz