Docker Machine is a great tool that is part of Docker itself, which is used for creating hosts to run Docker containers on.  It can do this locally in a virtual machine, or in the Cloud using various providers.   It is a Vagrant for Docker, if you like.  Note it is only in Beta and not recommended for production use yet.

I decided that this would be a great replacement for Vagrant and the AWS provider that I was using.  I’m working on Linux, and although I can run Docker natively and use my own machine as the host, I wanted to test what I was doing in a virtual machine so that it was clean and closer to the final hosting environment in AWS.  I have tried setting up Docker Machine under windows.  That was a trial and still isn’t working.  When I get that going I will blog about it separately.

Before you start you will need VirtualBox installed, and will probably need to stick with a 4.x version since 5.0 only just came out.  I followed the instructions on this link and was able to do:

$ docker-machine create --driver virtualbox dev

This creates a local virtual machine running under Virtualbox and gives it the name ‘dev’.  The machine is running boot2docker which is a tiny Linux that runs just from RAM, but has everything in it you need to run Docker.  If you need to log into the machine, you can do this:

$ docker-machine ssh dev

But most of the time you wouldn’t need to do this.  You can set up your environment so the docker command line operates on this docker virtual machine instead of your own machine.  To see how, type this:

(docker)rob@kirk ~/projects/hosting/docker $ docker-machine env dev
export DOCKER_HOST="tcp://"
export DOCKER_CERT_PATH="/home/rob/.docker/machine/machines/dev"
# Run this command to configure your shell: 
# eval "$(docker-machine env dev)"

The important one is the DOCKER_HOST variable – this tells docker where to connect to launch and manage containers.  So by typing this:

$ eval "$(docker-machine env dev)"

whenever you run docker now, it is pointing to your virtual machine:

(docker)rob@kirk ~/projects/hosting/docker $ docker run -d -p 8000:80 nginx
(docker)rob@kirk ~/projects/hosting/docker $ docker ps 
5f9a0faa0012 nginx "nginx -g 'daemon of 5 seconds ago Up 6 seconds 443/tcp,>80/tcp jolly_swartz

So now I’m running a container running nginx on a virtual machine running on my laptop.  How do I test that?

(docker)rob@kirk ~/projects/hosting/docker $ docker-machine ip dev

So if I connect to in my browser, then I see:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

In the next post I will introduce Docker Compose, which is another tool from Docker which will let me orchestrate the creation of sets of related containers.  For other posts in the series, go to the introduction.