Docker Swarm is a clustering and orchestration tool for managing Docker containers. It provides native clustering capability for Docker, allowing you to create and manage a swarm of Docker nodes. The nodes can be physical servers or virtual machines.

Docker Swarm can be deployed on any Linux system, making it a platform-independent tool. It is an excellent tool for managing large-scale containerized applications and is widely used in production environments.

Pre-requisites

  • Linux system with pre-installed Docker

Why use Docker Swarm?

Docker Swarm is designed to manage containerized applications at scale. It provides a unified interface for managing a cluster of Docker nodes, making it easy to deploy and manage containerized applications. Docker Swarm can be used for various applications, including web applications, micro-services, and big data processing.

You're probably wondering, "Why not Kubernetes?" It's because Kubernetes adds cost and management overhead. It is not feasible for small teams that don't need the entire Kubernetes package but only require some like scaling, healing, rollbacks, etc.

Setup Docker Swarm Cluster

Let's start deploying an application via Docker Swarm. In the following sections, we'll walk through the steps required to deploy a container image using Docker Swarm. I suppose you have Docker installed in your system and a basic understanding of Docker and containerization.

For this demo, we'll use two virtual machines. One will act as a manager node, and the other as a worker node.

✍️
Whether you choose virtual machines or cloud VPS for the nodes, the server provisioning and configuration are on you.

The IP of my manager node is 192.168.1.2, and the worker node is 192.168.1.3. Note that your IP address will be different from the ones we're using.

Ensure that the following ports are opened in both nodes for communication.

TCP port 2377 for cluster management communications
TCP and UDP port 7946 for communication among nodes
UDP port 4789 for overlay network traffic
Ports to open for Docker Swarm

In the manager node, run the following command.

docker swarm init --advertise-addr 192.168.1.2
Swarm initialized: current node (20t2cibtmjxcaxam1o0j7er4c) is now a manager.

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

    docker swarm join --token SWMTKN-1-5pfyqhf9zolbudgkxqmmrivx2ebfz9wqyvxs1ntv494h9096ww-anpgsk82gwtyy78xg8wajhbkz 192.168.1.2:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
Output – docker swarm init

You will now be presented with a token which you can use to connect the worker node with the manager node.

docker swarm join --token SWMTKN-1-5pfyqhf9zolbudgkxqmmrivx2ebfz9wqyvxs1ntv494h9096ww-anpgsk82gwtyy78xg8wajhbkz 192.168.1.2:2377
ℹ️
The token value is randomly generated by Docker and differs every time you execute the docker swarm init command.

Run the command below to check if our worker node has successfully joined the swarm.

docker info | grep -w -A 5 Swarm
Swarm: active
  NodeID: 1p5qdvjugyss8pvb1bqsy8myn
  Is Manager: false
  Node Address: 192.168.1.3
  Manager Addresses:
   192.168.1.2:2377
WARNING: No swap limit support
Output – docker info

On the manager node, run the command below to get information about nodes that have joined the specific swarm cluster.

docker node ls
ID                            HOSTNAME             STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
1p5qdvjugyss8pvb1bqsy8myn     docker-worker-node   Ready     Active                          23.0.1
29edvt4r11wbw7wo3o0qcl8fw *   docker-manager-node  Ready     Active         Leader           23.0.1
Output – docker node ls

Deploy Application to Docker Swarm

Services in Docker Swarm are like deployments in Kubernetes. It deploys the image and takes care of everything. The service name can be used to query all kinds of details of the application.

docker service create --name webserver_app --replicas 1 --publish published=8080,target=80,mode=host nginx:latest
ℹ️
If you are accessing the image from a private registry, you must perform docker login before entering the above command. Also, add the --with-registry-auth flag to provide the registry auth to the worker node during deployment only.

As I have taken an Nginx web server image to create the service, we can simply access it by entering the worker node's IP address.

Access the application in a web browser by entering the following URL

http://192.168.1.2:8080
URL to access the application

The above command displays the default page of the Nginx web server, as shown below.

Default Nginx page

Using the given command, you can check which nodes the workload is running on.

docker service ps webserver_app
Output

ID             NAME             IMAGE          NODE                 DESIRED STATE   CURRENT STATE             ERROR     PORTS
az90vp991t3w   webserver_app.1  nginx:latest   docker-worker-node   Running         Running 36 seconds ago              *:8080->80/tcp,*:8080->80/tcp
Output of docker service ps
ℹ️
If many replicas are requested, the management node will also host the applications, but usually, it's done by the worker node.

Advantages of Docker Swarm

Scalability

Docker Swarm provides a scalable platform for deploying containerized applications. It can easily handle thousands of nodes and containers, making it an ideal choice for large-scale applications.

Easy-to-Use

It is easy to use, with a simple and intuitive user interface. This makes managing and deploying containers effortless, even for developers new to Docker.

High availability

It provides high availability for containerized applications and automatically detects and recovers from node and container failures. This assures you about the status of your applications – Always up and running!

Security

Docker Swarm provides built-in security features, such as TLS encryption and access controls, protecting your applications and data from unauthorized access.

Cost-effective

By running multiple containers on a single machine, Docker Swarm can help reduce infrastructure costs. This optimizes resource utilization and reduces the need for additional hardware.

Disadvantages of Docker Swarm

Complex setup process

Docker Swarm can be difficult to set up and manage, especially for large-scale applications. It requires a good understanding of Docker, containerization concepts, networking, and security.

Limited features

Docker Swarm is a relatively new tool with fewer features than some more established container orchestration tools like Kubernetes.

Learning curve

If you're new to Docker and containerization, a steep learning curve can be associated with using Docker Swarm. It requires a good understanding of the Docker ecosystem and the principles of containerization.

Conclusion

In this article, we discussed Docker Swarm, Docker Swarm cluster set up and deployed a web server application to our Docker Swarm cluster. Next, we'll explore the concept of services in depth.  

Thank you for reading! Please comment below if you have any queries. I try to update my articles periodically to ensure legibility.