Since I last properly [wrote about Docker](/2014/08/31/docker-fig-nginx-reverse-proxies-and-centos-7/“Docker, Fig, NGINX Reverse Proxies and CentOS 7”) a lot has changed. Docker have introduced some new command line tools which allow for easy orchestration of Docker instances, clusters and container management. These are;
Docker Machine↗ — Allows you to easily deploy Docker instances to a lot of different platforms.
Rather than downloading the pre-compiled binary I decided to use the Homebrew↗formula (this assumes you have Cask installed);
Docker Machine, Compose & Swarm 1/39
This will install docker-machine;
Docker Machine, Compose & Swarm 2/39
I already have VirtualBox↗installed so lets create a machine called “Testing”;
Docker Machine, Compose & Swarm 3/39
docker-machine comes with a few commands which will help you connect using the locally installed docker client;
Docker Machine, Compose & Swarm 4/39
Thats it, I now have a Virtual Machine launched and ready for me to start using Docker;
Docker Machine, Compose & Swarm 5/39
As with any new installation, lets run a “Hello World”↗;
Docker Machine, Compose & Swarm 6/39
Finally you can SSH to the Virtual Machine using docker-machine ssh machine-name;
Docker Machine, Compose & Swarm 7/39
Great, so I now have a Virtual Machine running on my local computer, what more is there?
docker-machine is designed to be used with the following public and private cloud providers (more are being added all of the time);
Amazon EC2
Microsoft Azure
Digital Ocean
Google Compute Engine
Rackspace
SoftLayer
OpenStack
VMWare vCloud Air
VMWare vSphere
Lets use docker-machine to launch aDigital Ocean↗droplet. To do this you will need to generate a Personal Access Token, you can do this byfollowing these instructions↗. Once have the token launch the droplet as follows;
Docker Machine, Compose & Swarm 8/39
(and no, that is not my Digital Ocean Personal Access Token, it’s just a random string)
So what just happened? docker-machine accessed my Digital Ocean account via the API and launched the following Droplet;
OS = Ubuntu 14.04 x64
RAM = 512MB
HDD = 20GB SSD
Region = NYC3
These defaults can be over-ridden by providing more options, run docker-machine create — help for a full break down of the options along with example arguments.
Once the droplet had finished booting, docker-machine connected to the droplet via SSH installed, configured and started the latest version of Docker.
So, we now have two machines launched, one locally and one in Digital Ocean;
Docker Machine, Compose & Swarm 9/39
lets run the “hello world” again, but this time use the droplet which has just been launched;
Docker Machine, Compose & Swarm 10/39
and finally SSH into the machine;
Docker Machine, Compose & Swarm 11/39
Finally, you can stop and remove machines using docker-machine stop machine-name and docker-machine rm machine-name, be careful when using rm as it does not prompt you if you are sure;
Docker Machine, Compose & Swarm 12/39
So thats a quick overview of docker-machine. As you can see, it is a a really convenient way to bootstrap Docker server instances across many different providers and tear them down all using a single command from your local machine.
Docker Compose started life as Fig which is something [I have written about before in a previous post](/2014/08/31/docker-fig-nginx-reverse-proxies-and-centos-7/“Docker, Fig, NGINX Reverse Proxies and CentOS 7”), the currently release doesn’t add too much in the way of new functionality, but it does start laying the foundations for working with docker-swam,click here↗for the full release notes.
Like docker-machine I installed it using aHomebrew↗formula;
Docker Machine, Compose & Swarm 13/39
So using docker-machine lets create a Docker server instance to use docker-compose with;
Docker Machine, Compose & Swarm 14/39
as docker-compose doesn’t interact directly with docker-machine we need to tell the main docker client the details of the server instance which has just been launched;
Docker Machine, Compose & Swarm 15/39
this command injects the environment variables needed for the docker client to connect to the server instance, to see these you can just run docker-machine env machine-name on it’s own;
Docker Machine, Compose & Swarm 16/39
From here, it is just like Fig, apart from fig.yml file should now be called docker-compose.yml, I had a fig.yml file from a previous post still on my machine;
Docker Machine, Compose & Swarm 17/39
It launches two containers and links them together along with mounting the ./web folder in the NGINX container. The directory structure of the folder I am going to be running docker-compose from looks like;
Docker Machine, Compose & Swarm 18/39
To start with I pulled the images which are going to be launched, you can ignore this part, it just makes showing whats going on in this post more straight forward;
Docker Machine, Compose & Swarm 19/39
Now the images have been pulled down it’s time to launch the containers;
Before going any further, the documentation warns ….
Note: Swarm is currently in beta, so things are likely to change. We don’t recommend you use it in production yet.
Now that’s out of the way lets useHomebrew↗to install docker-swarm;
Docker Machine, Compose & Swarm 25/39
As we already have docker-machine installed I will be using it to locally create the cluster, first of all we need to launch an instance and run the swarm container;
Docker Machine, Compose & Swarm 26/39
As you can see, I got a token when the container launched 63e7a1adb607ce4db056a29b1f5d30cf I will need this to add more nodes, but first we will need to create a Swarm master;
Docker Machine, Compose & Swarm 27/39
We then need to connect the Docker client to the swarm, this is done by adding — swarm to the $(docker-machine env machine-name) command;
Docker Machine, Compose & Swarm 28/39
Now lets add another node;
Docker Machine, Compose & Swarm 29/39
We now have a 2 node cluster called “swarm-master”;
Docker Machine, Compose & Swarm 30/39
Using docker info gives more information about the cluster;
Docker Machine, Compose & Swarm 31/39
Great, so what does all of this mean?
Lets pull some images down;
Docker Machine, Compose & Swarm 32/39
Notice how I have pulled redis on “swarm-master” and mysql on “swarm-node-00”, I can now make sure that containers only launch on a node where the image is available;
Docker Machine, Compose & Swarm 33/39
another example would be ports being used on a node, lets pull my NGINX & PHP image on both nodes;
Docker Machine, Compose & Swarm 34/39
Now lets launch the a container and bind it to port 80;
Docker Machine, Compose & Swarm 35/39
and again;
Docker Machine, Compose & Swarm 36/39
Nothing special there you say? Well, normally, when trying to launch the second container you would have gotten the following error as you can not bind two containers to the same port;
Docker Machine, Compose & Swarm 37/39
However, in this case as Docker is aware of what is running on the nodes wothin the cluster including which ports are in use. Docker via Swarm simply launched the container on “swarm-node-00” and it knew that port 80 was already in use on “swarm-master”;
Docker Machine, Compose & Swarm 38/39
All of this was done with no prompting or special commands, it helpfully just got on with it
As you can see, docker-swarm is still very much in-development and there are some deal breakers, like containers not being able to talk to each other across nodes. However, with the news thatsocketplane.io↗(they produce a container based software defined networking solution using Open vSwitch) isjoining Docker↗I don’t think it will be too long before this problem is resolved.
Finally, lets remove the running instances;
Docker Machine, Compose & Swarm 39/39
That’s it for now, expect a follow up post in the next few months as these tools are updated.