Getting Started With Docker Swarm

Multi-container, multi-machine applications are made possible by joining multiple machines into a “Dockerized” cluster called a swarm. A swarm is a group of machines that run Docker and join into a cluster.

In a swarm, we run Docker commands that are executed on a cluster by a swarm manager. The machines in a swarm can either be physical or virtual. After joining a swarm, they are referred to as nodes.

Swarm managers are the only machines in a swarm that can execute your commands, or authorizes other machines to join the swarm as workers. Workers are just there to provide capacity and do not have the authority to tell any other machine what it can and cannot do.

Workflow diagram of Docker swarm

Setting up Swarm
A swarm is made up of multiple nodes, which can be either physical or virtual machine. The basic concept is simple enough: run docker swarm init to enable swarm mode and make your current machine a swarm manager, then run docker swarm join on other machines to have them join the swarm as workers.

Create 3 virtual machines using the following command:
docker-machine create –driver virtualbox virtual_machine_name

Virtual machine 1: jarvis
docker-machine create –driver virtualbox jarvis

Virtual machine 2: genisys
docker-machine create –driver virtualbox genisys

Virtual machine 3: unicorn
docker-machine create –driver virtualbox unicorn

You now have three VMs: jarvis, genisys, and unicorn (as docker-machine ls shows). The first one will act as the manager, which executes docker commands and authenticates workers to join the swarm, and the second, the third will be the workers.

You can send commands to your VMs using docker-machine ssh. Instruct jarvis to become a swarm manager with docker swarm init.

Copy the IP address for jarvis by running docker-machine ls, then run the docker swarm init command, using that IP and specifying port 2377 (the port for swarm joins) with –advertise-addr.

Making jarvis as a Swarm manager:
docker-machine ssh jarvis “docker swarm init –advertise-addr”

and you’ll see an output like this:

Swarm initialized: current node (yci3dfhta6csc6wnz2qg094f3) is now a manager.

To add a worker to this swarm, run the following command:

docker swarm join –token SWMTKN-1-5am1wqbeaw80j57x6sd2tn5ebgsko5pldyoptnq1hpen8rprpj-8fv1pqepzpocrc0actbucj0m3

To add a manager to this swarm, run ‘docker swarm join-token manager’ and follow the instructions.

As you can see, the response to docker swarm init contains a pre-configured docker swarm join command for you to run on any nodes you want to add. Copy this command, and send it to genisys and unicorn via docker-machine ssh to have genisys and unicorn join your new swarm as workers.

Making genisys as a worker:

docker-machine ssh genisys “docker swarm join –token SWMTKN-1-5am1wqbeaw80j57x6sd2tn5ebgsko5pldyoptnq1hpen8rprpj-8fv1pqepzpocrc0actbucj0m3”

Making unicorn as a worker:

docker-machine ssh unicorn “docker swarm join –token SWMTKN-1-5am1wqbeaw80j57x6sd2tn5ebgsko5pldyoptnq1hpen8rprpj-8fv1pqepzpocrc0actbucj0m3”

Congratulations, you have created your first swarm!

Use ssh to connect to the (docker-machine ssh jarvis), and run docker node ls to view the nodes in this swarm. You see output like this:

Type exit to get back out of that machine.

Deploy your App on the cluster:
Only swarm managers like jarvis execute Docker commands; workers are just for capacity.

Copy the file docker-compose.yml to the swarm manager jarvis’s home directory (alias: ~) by using the docker-machine scp command:


Docker-compose.yml :

version: "3"
   image: mkkasturi/app:1.0
     replicas: 7
       condition: on-failure
         cpus: "0.1"
         memory: 50M
     - "80:80"
     - webnet
   image: dockersamples/visualizer:stable
     - "8080:8080"
     - "/var/run/docker.sock:/var/run/docker.sock"
     - webnet

docker-machine scp docker-compose.yml jarvis:~

Now, deploy your app with jarvis as the swarm manager with docker stack deploy command using docker-machine ssh:

docker-machine ssh jarvis “docker stack deploy -c docker-compose.yml wavelabs”

And that’s it, the app is deployed on a cluster!

Execute this command, you’ll see that the containers have been distributed among jarvis, genisys, and unicorn.

docker-machine ssh jarvis “docker stack ps wavelabs

Open the browser to see the visualizer using the IP address of any machine in the swarm with port number 8080.

You can see 7 containers of your application and one container for Visualizer.

Accessing your cluster
You can access your app from the IP address of either jarvis, genisys or unicorn. The network you’ve created is shared between them and load-balancing.

Source: Certain sections of this post are taken from