Setting up a Docker host on OSX with Vagrant

The instructions on the Docker web page use the Docker Toolbox, but we can also build a Docker host using Vagrant(to provision virtual machines) and VirtualBox (to host virtual machines). Doing this helps us understand how everything fits together.

  1. Install Oracle VirtualBox
  2. Install Vagrant
  3. Open a new terminal window and create a directory for our vagrant box:
cd ~ && mkdir -p vagrant/trusty64 && cd vagrant/trusty64

Download the Vagrantfile for ubuntu (trusty 64-bit)

vagrant init ubuntu/trusty64

The Vagrantfile contains instructions for how Vagrant should build your virtual machine. Open the Vagrantfile with your text editor of choice and find the following sections:

# using a specific IP.
# config.vm.network “private_network”, ip: “192.168.33.10”
...
# config.vm.provision “shell”, inline: <<-SHELL
# sudo apt-get update
# sudo apt-get install -y apache2
# SHELL

Modify it so that Vagrant assigns the IP address “192.168.10.101” and installs Docker for us:

# using a specific IP.
config.vm.network “private_network”, ip: “192.168.10.101”
...
config.vm.provision “shell”, inline: <<-SHELL
wget -qO- https://get.docker.com/ | sh
SHELL

After the edits, the file should look like so:

Vagrantfile with comments removed (custom cpu/memory settings are optional)

Back in the terminal, we can bring the virtual machine online by running:

vagrant up

Once the Vagrant box is ready, ssh to it:

vagrant ssh

Now we can start running docker images!

sudo docker run -d -p 80:80 nginx 

Remember that IP address we set earlier? Now we can alias it in our hosts file so that it’s easier to remember. Open /etc/hosts and add the following line:

192.168.10.101 dockerbox

In OSX, open a browser and navigate to http://dockerbox and you should see the nginx welcome page.

Congratulations, you just created a local docker host and deployed nginx!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.