Last time we took a look on how to setup local swarm with Docker 1.21. This time we will extend that a bit and see how to use Docker Machine to
accomplish the same setup.
Docker Machine allows to specify where our environment will be running by using different “drivers” to provision multiple remote Docker hosts on various
flavors of Linux. At the moment big set of drivers is supported, in our example we will use Digital Ocean
as a host for swarm nodes. To follow the steps described here you will need a DO account and DO access token so that you can create droplets
via DO API. In case you don’t have a DO account, you can use this link to subscribe and you will receive 10$ DO credit!
Let’s first go and see how to spin up a docker client on a new DO droplet with Docker Machine. Simply, by running following command:
Will do following steps:
This will create a minimal DO droplet (512MB one, at the moment of this post) based on Ubuntu 16.04.1, it will configure SSH keys and certificates and install
latest docker on it! How simple and powerful was that, huh? You can connect to your DO console and you will see your droplet up and running.
To connect your shell to your new droplet with Docker Client, lets run
eval $(docker-machine env test-this). Now every docker command in this session
will be executed on your droplet.
Lets test this and create some containers on this droplet! E.g. Nginx is a nice one to try,
docker run -d -p 8000:80 nginx will be done quite
quickly and usual
docker ps will show our container up and running:
I know what are you thinking now, “too easy to be true!", well that was my thinking also so lets see does this really run on our droplet
docker-machine stop test-this
Now this should stop your droplet and if the previous
docker ps and container was really executed on the droplet, it should now be offline, lets
check again with
It worked! Our DO console will also confirm that our droplet is stopped. As we will now start playing with swarm and create a new setup for that, lets remove
this test Docker Machine instance with
docker-machine rm test-this. You will get a simple prompt that asks are you sure, confirming that will remove
your DO droplet and Docker Machine that was created.
Now lets set up a swarm cluster with Docker Machine on DO with one swarm manager and two nodes, according to the official documentation, this is as simple as:
So 5 minutes later and our swarm is up and running! Lets test it, first lets connect our shell with swarm-manager:
If you now try to use your swarm with
docker node ls you will get:
Wait what? But my droplets are running, docker info is reporting containers running, where is the magic?!? Well it turns out that docker machine is still creating
old version of swarm, not the docker 1.21 swarm. Ugh! This will probably be updated in new versions but in the current (at the moment of this post) Docker Machine
0.8.1, to create new type of swarm based on docker 1.21 you need to do following steps.
First delete three droplets that you just created :-) with
docker-machine rm swarm-manager-01,
docker-machine rm swarm-node-01 and
docker-machine rm swarm-node-02.
Now lets create new nodes:
So three clean nodes with Docker Client are up and running, check, lets now configure swarm on them:
Good, so swarm manager is now up and running, lets add our nodes to the swarm:
So now after
eval "$(docker-machine env swarm-manager-01)" if you try to see the status of your swarm with
docker node ls you will get:
Yay! It looks like it worked. How about we put some load on this swarm :-)
docker service create --name web --replicas=10 -p 30000:80 nginx
This should create nginx service on your shiny new swarm with 10 replicas, you can check that with
docker service ls and you should get:
Well that’s all for now, just a small reminder that you delete your Docker Machines on DO after you are done with playing with
docker-machine rm [node-name]
to save some $$ and make sure that they are actually stopped and deleted by checking your DO droplets page.