Docker Swarm mode setup

Tutorial on how to setup docker 1.21 in swarm mode

Perica Zivkovic

3minute read

Starting with version 1.21, docker engine includes native support for “swarm” mode. Newly introduced services can now be deployed, scaled and managed on the docker swarm cluster. Cluster management and orchestration commands are directly embedded in docker engine but you can still use docker engine in “standalone” mode to manage individual containers.

There is already an excellent getting started guide provided by official docker documentation that can help you in understanding main concepts of the swarm mode.

I have prepared four Ubuntu 16.04 VM’s that will act as a nodes in Swarm cluster. One node will act as a “master node” dmaster1 and other three dnode1, dnode2 and dnode3 will be worker nodes.

To install docker engine on these vanilla Ubuntu 16.04 servers, you can follow official documentation, or use this pre-cooked script:

sudo apt-key adv --keyserver hkp:// --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
echo "deb ubuntu-xenial main" | sudo tee /etc/apt/sources.list.d/docker.list
sudo apt-get update
sudo apt-get install -y linux-image-extra-$(uname -r)
sudo apt-get install -y docker-engine
sudo service docker start

To test your installation you can run sudo docker run hello-world and you should see following message if your installation was successful:

Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker Hub account:
For more examples and ideas, visit:

Next steps are creating the Swarm and with docker 1.21 this is super simple (comparing to older docker versions) by executing “swarm init” on the master node:

sudo docker swarm init --advertise-addr <MANAGER-IP>

where MANAGER-IP is ip address of your master server. Output of this command will immediately give you commands that you need to run on your worker nodes or on additional master nodes, in case you would like to create HA setup, so that they can be added to your cluster, e.g.

Swarm initialized: current node (dxn1zf6l61qsb1josjja83ngz) is now a manager.
To add a worker to this swarm, run the following command:
    docker swarm join \
    --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
To add a manager to this swarm, run the following command:
    docker swarm join \
    --token SWMTKN-1-61ztec5kyafptydic6jfc1i33t37flcl4nuipzcusor96k7kby-5vy9t8u35tuqm7vh67lrz9xp6 \

After executing appropriate join commands on the worker nodes we have our swarm cluster ready for first services

perica@dmaster1 ➜  ~ sudo docker node ls
39vfad94n4iz8sadjk6yzmi5y    dnode1    Ready   Active        
40kefl5citf9ej4t6gc97qqdr    dnode3    Ready   Active        
8zcet33teixl63tj66at338v3    dnode2    Ready   Active        
b2vf4qy6sq1sc56mwole8zngc *  dmaster1  Ready   Active        Leader
comments powered by Disqus