You can support us by downloading this article as PDF from the Link below. Download the guide as PDF

Docker swarm is a tool used to create a cluster of docker hosts. With docker swarm, we can create a high availability and high performance cluster where applications are distributed among the hosts. Docker swarm consists of a manager and worker nodes and operations are performed from the manager. In this guide, we are looking at how to set up Docker swarm cluster on Ubuntu 20.04.

Step 1: Prepare your nodes.

In my set up, I have one manager node and two worker nodes. On each host, configure hosts file to include all the other nodes.

sudo vim /etc/hosts

Add the following content to the file	manager	worker-01	worker-02

Save the file. Ensure that you can ping all the hosts from each host using hostname and not IP address.

Step 2: Install Docker CE on Ubuntu 20.04

We are going to install Docker CE on the hosts. To install Docker CE on Ubuntu 20.04, following the following steps:

Install dependency packages with the below command

sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common

Add docker key and docker repository to your hosts with the following commands:

curl -fsSL | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] focal stable"

Update packages

sudo apt-get update

Ensure that you are going to install from the official Docker repo instead of default Ubuntu repo.

$ apt-cache policy docker-ce
 Installed: (none) 
 Candidate: 5:19.03.13~3-0~ubuntu-focal 
 Version table: 
    5:19.03.13~3-0~ubuntu-focal 500 
       500 focal/stable amd64 Packages 
    5:19.03.12~3-0~ubuntu-focal 500 
       500 focal/stable amd64 Packages 
    5:19.03.11~3-0~ubuntu-focal 500 
       500 focal/stable amd64 Packages 
    5:19.03.10~3-0~ubuntu-focal 500 
       500 focal/stable amd64 Packages 
    5:19.03.9~3-0~ubuntu-focal 500 
       500 focal/stable amd64 Packages

Install Docker CE on Ubuntu 20.04

sudo apt install docker-ce

After installing, Docker daemon should be started. Confirm the status by running the below command:

$ sudo systemctl status docker
● docker.service - Docker Application Container Engine 
    Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) 
    Active: active (running) since Sat 2020-10-17 16:28:08 EAT; 57s ago 
TriggeredBy: ● docker.socket 
  Main PID: 2807 (dockerd) 
     Tasks: 8 
    Memory: 37.5M 
    CGroup: /system.slice/docker.service 
            └─2807 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Add your user to Docker group to avoid typing sudo everytime you run docker commands.

sudo usermod -aG docker ${USER}

Step 3: Create Docker Swarm Cluster

To set up swarm cluster, we first need to initialize Docker Swarm mode on the manager node then add the worker nodes to the cluster. Run the below command to initialize Docker swarm node on the manager.

$ sudo docker swarm init --advertise-addr

Swarm initialized: current node (fsuaqqpihi2eabmmq8gldzhpv) is now a manager. 
To add a worker to this swarm, run the following command: 
sudo docker swarm join --token SWMTKN-1-018kvdektwa74z8fajb5c1u6jyz6qfk4ood8u4qotw7go9jj0p-cfpnh7omy86xcgoh45vau2kaj
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

We are now going to add the ‘worker-01‘ and worker-02 nodes to the cluster ‘manager’ using the ‘join-token’ from the cluster ‘manager’ node. Run the command printed in the init command output.

$ sudo docker swarm join --token SWMTKN-1-13xo81gxpb3ttjh5e335pfrmz9fbnliikgfys7u8l4r8k4m575-2gsjwjs3y1i4kgeua2yu840hw 
This node joined a swarm as a worker.

Check if the worker nodes are successfully added to the cluster by running the below command on the manager:

$ sudo docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION 
etx5xc5guzftmsqx4naqwvump     worker-02           Ready               Active                                  19.03.13 
xop4jvrkywz4ldsgwqmfacssc *   manager             Ready               Active              Leader              19.03.13 
bghrkp7ll1b9lb0ikv8x51gzy     worker-01           Ready               Active                                  19.03.13

Step 4: Deploy application in the Cluster

Let’s create a service Nginx web server to run on default http port 80, and then expose it to the port 8080 on the host.

$ sudo docker service create --name web-server --publish 8080:80 nginx:1.13-alpine
overall progress: 1 out of 1 tasks  
1/1: running   [==================================================>]  
verify: Service converged 

Confirm the created service by running the below command:

$ sudo docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
pq5txw0p9c1q        web-server          replicated          1/1                 nginx:1.13-alpine   *:8080->80/tcp

Step 5: Replicas and Scaling the service.

We are going to make 3 replicas of the web-server service so that it is accessible on the manager and the two worker nodes.

$sudo docker service scale web-server=3
web-server scaled to 3 
overall progress: 3 out of 3 tasks  
1/3: running   [==================================================>]  
2/3: running   [==================================================>]  
3/3: running   [==================================================>]  
verify: Service converged 

Confirm created service replicas

$ sudo docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS 
pq5txw0p9c1q        web-server          replicated          3/3                 nginx:1.13-alpine   *:8080->80/tcp

Access the service from your browser using all the nodes IPs:, and and you should get a nginx welcome page as below:

Set up docker swarm on ubuntu 20.04

This has been a step-by-step guide on how to set up Docker Swarm on Ubuntu 20.04. I believe it has been helpful. Check more interesting guides below:

As an appreciation for the content we put out,
we would be thrilled if you support us!

As we continue to grow, we would wish to reach and impact more people who visit and take advantage of the guides we have on our blog. This is a big task for us and we are so far extremely grateful for the kind people who have shown amazing support for our work over the time we have been online.

Thank You for your support as we work to give you the best of guides and articles. Click below to buy us a coffee.


Please enter your comment!
Please enter your name here