How to Install and Configure Docker Swarm on Ubuntu 22.04

This tutorial will show you how to install and configure Docker Swarm on Ubuntu 22.04 ensuring that you harness its full potential for your containerized applications. For effectively scaling and managing containers, Docker Swarm is still a great option. Many developers and DevOps experts prefer it because of its ease of use and power, which allows you to deploy and manage containers seamlessly.

Before we get started with the installation and configuration steps, let’s quickly go over what Docker Swarm is and why you might want to use it.

What is Docker Swarm?

Docker Swarm is a native clustering and orchestration solution for Docker containers. It allows you to create and manage a cluster of Docker nodes, turning them into a single virtual Docker host. This unified interface simplifies the deployment and scaling of containerized applications while providing high availability and load balancing.

Why Choose Docker Swarm?

There are many options for container orchestration, including Kubernetes and Apache Mesos. So what justifies choosing Docker Swarm?

  1. Simplicity: Docker Swarm is known for its user-friendly and straightforward setup. It is an excellent choice for those new to container orchestration.
  2. Compatibility: If your infrastructure is already built on Docker containers, Docker Swarm seamlessly integrates with your existing environment.
  3. Scaling: Swarm is designed to scale your application horizontally with ease, distributing containers across multiple nodes to ensure high availability.
  4. Load Balancing: It offers built-in load balancing, directing traffic to healthy containers and ensuring minimal downtime.

Let’s begin the process of installing and configuring Docker Swarm on Ubuntu 22.04.


Make sure you have the following prerequisites in place before beginning the installation process:

  1. Two Ubuntu 22.04 Virtono Servers: You should have a clean installation of Ubuntu 22.04 on your server.
  2. Root Access: You need to have root access or be part of the sudo group to execute commands with administrative privileges.
  3. Docker Installed: Ensure that Docker is already installed on your Ubuntu server. If not, you can refer to our previous article on how to Install Docker on Ubuntu 22.04.
  4. Static IP Address: It’s recommended to have a static IP address assigned to your server to prevent IP changes that could disrupt your Docker Swarm setup.

Installing Docker Swarm on Ubuntu 22.04

It’s important to upgrade the installed packages and update your system’s package list before installing any software. Run the following commands:

sudo apt update && apt upgrade -y

This ensures that your system has the most recent security patches and updates.

To start using Docker Swarm, you need to initialize it on your Docker host. Run the following command:

sudo docker swarm init
How to Install and Configure Docker Swarm on Ubuntu 22.04

To confirm that your Docker Swarm cluster is up and running, you can use the following command on the manager node:

sudo docker node ls
Docker Swarm on Ubuntu Leader

This command will set up the current node as the swarm manager. It will also provide you with a token that you can use to join worker nodes to the swarm.

Join Worker Nodes

Use the token provided in the preceding step to add additional nodes to your Docker Swarm on Ubuntu cluster if you have any. Run this command on every worker node:

sudo docker swarm join --token TOKEN MANAGER-IP:2377

Replace TOKEN with the token you received from the manager and MANAGER-IP with the IP address of your swarm manager.

Check the manager node to see if each worker node has joined the cluster successfully.

sudo docker node ls
Docker Swarm on Ubuntu list nodes

Configuring Docker Swarm on Ubuntu

In Docker Swarm on Ubuntu, a service is a fundamental construct for defining the desired state of your application. It specifies which containers should run, how many replicas should be maintained, and various other settings.

Here’s an example of how to create a service:

sudo docker service create --name virtono-tutorial --publish 80:80 docker/getting-started

This service will run on the standard HTTP port 80 and be exposed to port 80 on the host server.

Configure Docker Swarm on Ubuntu

Verify the status of the created service:

Docker Swarm on Ubuntu service

Create Replicas of the Service

We will create two replicas of the service using the two nodes in our cluster. This will make it possible for both manager and worker nodes to access the service.

sudo docker service scale virtono-tutorial=2
how to create replicas Docker Swarm on Ubuntu

Open your browser and access your IP to check your service on the web.

How to Install and Configure Docker Swarm on Ubuntu


Built-in overlay networking in Docker Swarm on Ubuntu enables seamless communication between containers in various parts of your swarm. For simple container-to-container communication, it also provides DNS-based service discovery.

High Availability and Load Balancing

The fact that Docker Swarm on Ubuntu can automatically provide high availability and load balancing is one of its main benefits. The built-in load balancer makes sure that traffic is directed to healthy containers while allowing services in a swarm to be distributed across multiple nodes.

Monitoring and Scaling

It’s essential to monitor your Docker Swarm on Ubuntu cluster to guarantee the functionality and performance of your applications. Prometheus and Grafana are two applications you can use to collect and display metrics. In order to handle increased traffic and load, Docker Swarm also makes it simple to scale your services horizontally.

Final Thoughts

You can effectively deploy, manage, and scale your containerized applications by following the instructions provided in this article. Docker Swarm on Ubuntu reduces the complexity of container orchestration with features like high availability, load balancing, and simple service management.

As you learn more about Docker Swarm, you’ll see how flexible and scalable it is, making it an important tool in your containerization toolbox.

Start using Docker Swarm to orchestrate your containers on Ubuntu 22.04 right away to enjoy the advantages of more efficient container management.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.