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_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.99.100:2376" export DOCKER_CERT_PATH="/home/rob/.docker/machine/machines/dev" export DOCKER_MACHINE_NAME="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 5f9a0faa00123ccd166daac1454241a01946e13449daa2ca8cd8acd021b500f7 (docker)rob@kirk ~/projects/hosting/docker $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5f9a0faa0012 nginx "nginx -g 'daemon of 5 seconds ago Up 6 seconds 443/tcp, 0.0.0.0:8000->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 192.168.99.100
So if I connect to http://192.168.99.100:8000 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.