Install and Configure Docker Swarm on Ubuntu 20.04 LTS

Updated on June 28, 2021
Install and Configure Docker Swarm on Ubuntu 20.04 LTS header image


Docker Swarm is a container orchestration tool. That is, it allows for creation of a cluster of docker hosts and turns them into a single virtual server. This enables your application running on containers achieve high performance and high availability by distributing it between available hosts within the cluster. You will learn how to install and configure docker swarm in this article.


1. Configure Cluster Hosts

For whichever number of servers deployed, one will be the manager node and the rest worker nodes. Manager node handles all cluster management tasks while the worker nodes run the containers. In this article, we will use two nodes, for example: manager worker-1
  1. SSH to all Ubuntu server as a non-root user with sudo access.

  2. Edit host file /etc/hosts in all the nodes.

     $ sudo nano /etc/hosts
  3. Add the following code in the file. manager worker-1
  4. Ping all the nodes using their hostnames.

     $ ping -c 4 manager
     $ ping -c 4 worker-1

2. Install Docker CE

Install Docker CE on all the nodes. Perform all stages in this step on all nodes.

  1. Update the system packages.

     $ sudo apt update
  2. Install all required packages.

     $ sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
  3. Install Docker repository signing keys.

     $ curl -fsSL | sudo apt-key add -
  4. Add the Docker repository.

     $ sudo add-apt-repository "deb [arch=amd64] focal stable"
  5. Update the system packages.

     $ sudo apt update
  6. Install Docker CE.

     $ sudo apt install docker-ce -y
  7. Confirm the status of Docker.

     $ sudo systemctl status docker
  8. Enable the Docker service to automatically run at system boot.

     $ sudo systemctl enable docker
  9. Add your current user to docker group.

     $ sudo usermod -aG docker ${USER}

3. Create Docker Swarm Cluster

To create a Docker Swarm cluster, you will first need to initialize the swarm mode on the manager node. Then, join the worker nodes to the cluster. Strictly use the nodes IP address.

  1. Initialize the swarm mode.

     $ sudo docker swarm init --advertise-addr
  2. Go to the worker-1 node and add it to the cluster. Modify the --token value with your own.

     $ sudo docker swarm join --token SWMTKN-1-2jxta71638d1pyioznb9jo4hi4u5ppd8t7lc90linwi9acu54s-aef4mqdy23ktrkcxsp57uyoma
  3. Go to the manager node and verify if all the worker nodes successfully joined the cluster.

     $ sudo docker node ls

4. Deploy Application to the Cluster

  1. Go to the manager node and create a 'Docker getting started web-page' service named docker-tutorial that will run on default http port 80 and expose it to port 80 on the host server.

     $ sudo docker service create --name docker-tutorial --publish 80:80 docker/getting-started
  2. Verify the status of the created service.

     $ sudo docker service ls

5. Create Replicas of the Service

With two nodes in our cluster, we will make two replicas of the service. This will enable access to the service form both the manager and worker nodes.

  1. Create replicas.

     $ sudo docker service scale docker-tutorial=2
  2. Verify the status of the service replicas.

     $ sudo docker service ls
  3. Go to your browser and access the service from all your nodes. For example:

    manager node.

    worker-1 node.


You have successfully installed and configured Docker Swarm. You can now scale your application by adding more worker nodes to the cluster and creating more replicas of the services you have running.