Docker series (part 4 of 4): Docker Swarm

08 mei 2020 om 15:01 by ParTech Media - Post a comment

In the previous post, we found Docker Compose to be a simple and user-friendly tool that helps in managing multiple containers with ease. This justifies its popularity in the developer world.

However, Docker Compose has a major limitation - it is incapable of managing containers in multiple hosts.

Thankfully, there is a solution: Docker Swarm, a container orchestration tool that can help you to manage multiple containers in multiple host systems by forming a swarm.

In this post, we are going to understand more about this solution - what a swarm is, how does a Docker Swarm work, and the benefits offered by it. We will also look into some simple commands to create a swarm and how to deploy services in it.

Table of Contents

  • What is a Docker Swarm?
  • Two Types of Nodes
  • How does a Docker Swarm work?
  • Why Docker Swarm?
  • Basic Commands in Docker Swarm

What is Docker Swarm?

A Docker Swarm is a container orchestration tool that helps you to manage multiple containers in multiple hosts. The cluster of hosts or the collection of physical machines is called a swarm. A swarm can also be multiple virtual machines running on a single host and joined together to form a cluster.

Once the machines have been clustered, you will be able to run Docker commands in the machines constituting a cluster.

Since Docker Swarm is included as part of the Docker Engine, you can use the Docker Command Line Interface to create a swarm, deploy the services to swarm and manage how the swarm behaves.

Two Types of Nodes in a swarm

A node is just an instance of the Docker engine that effectively participates in a swarm.

Typically, the activities of a cluster are managed by a swarm manager(manager node) which controls a set of machines called worker nodes.

1) Manager Node

A manager node distributes, schedules, and assigns tasks to the worker nodes in a swarm. It also runs services optionally for the worker nodes. Docker recommends a maximum of seven manager nodes in a swarm. You can create a swarm of just manager nodes without a worker node, but the vice versa is not possible.

2) Worker Node

A worker node takes instruction from a manager node and executes containers and services. By default, every manager node is also a worker node and can execute their tasks if they have the respective resources.

How does a Docker Swarm work?

Recall a service definition from the YAML file we created for Docker Compose. A service is nothing but defining the executable tasks for nodes including information such as which containers to use and what commands to execute inside a container.

Whenever you deploy a service to the swarm, the manager node considers your definition of the service as the desired state. Accordingly, it schedules this service on each worker node in the form of similar tasks or replica tasks. Each replica task invokes a container and runs independently of each other. For example, when you define a service that instructs the Docker Swarm to run three instances of a database, Docker Swarm creates three tasks.

Why Docker Swarm?

One of the reasons why Docker Swarm was created is to mitigate an important vulnerability offered by a single host system.

Imagine a situation where a solution has been deployed on a single host and this host becomes unhealthy leading to the failure of the entire solution. Now unless you have a contingency environment, you will no longer be able to use the service for the purpose it was created.

This is where a Docker Swarm helps as it offers a multi-node environment enabling us to distribute our services across multiple hosts. Even if one host fails, the other hosts can continue to function thereby providing uninterrupted service.

For example, in the below swarm, if worker node 1 fails, then the Docker Swarm automatically assigns the containers of node 1 to node 2.


In the above scenario, you saw what happens when a worker node fails, but what if the manager node fails especially if there is only a single manager node. In such cases, the containers will continue to run in their respective worker node, however, you will have to create a separate cluster to recover the services of the failed manager node to restore to its original state. This will lead to unnecessary downtime.

To avoid downtime in the recovery process, Docker recommends an odd number of manager nodes to be implemented. For example, set up 3 manager nodes to tolerate the loss of one manager node without the need for downtime. Similarly, 7 managers to tolerate 2 manager nodes and so on.


There are various other benefits of using Docker Swarm. For example, Docker Swarm helps to utilize containers and all the advantages it offers. Similarly, load balancing is another important benefit of using Docker Swarm. Docker Swarm schedules tasks in such a way that every container has enough resources to work. The swarm manager ensures that the workloads of the containers are assigned to run on the most optimal host for the best efficiency.

Basic Commands in Docker Swarm

Now let us see some basic commands that will help you to create a swarm, add nodes to it, promote a node, and leave the swarm.

First of all, to create a swarm, use the below command in the CLI of Docker Desktop:

docker swarm init --advertise-addr

The --advertise-addr command here instructs the Docker Swarm to configure the IP address of the manager node as This is the address in which all the other nodes in the swarm can access this manager node. The response to this command will be:

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

Here ******* denotes a token that is automatically generated by Docker Swarm.

To add a worker node to this swarm, use the below command:

docker swarm join --token SWMTKN-1-****-***

To add a manager node to this swarm, use the below command and follow the instructions:

docker swarm join-token manager

You can also promote a worker node to a manager node using the below command:

docker node promote worker1

Here worker1 is the name of the 2nd host added to a swarm.

To leave the swarm, use the below command:

docker swarm leave

To deploy a service to a node, use the below command:

docker service create --replicas 3 --name helloworld partech ping

Here helloworld is the name of the service and partech is the name of the image. Three replica tasks will be created for the service helloworld.


Our journey in this series began with setting up Docker Desktop followed by creating a container and managing it using Docker Compose. In this post, we have seen how Docker Swarm can be used instead of Docker Compose to manage multiple containers in multiple host systems which is not possible in Docker Compose. Docker Swarm also offers various other benefits over Docker Compose such as load balancing and the ability to run the services even after a node fails.