Build Your First Docker Swarm Cluster#
If you’re new to Docker and eager to dive into container orchestration, you’ve come to the right place. Before jumping into Docker Swarm, itโs essential to first understand what Docker is and how Docker Compose works. These foundational tools will help you grasp the power of Docker Swarm more effectively.
๐ Recommended Reads First:#
What is Docker? โ A beginner’s guide to containers.
https://systemadmin-insights.hashnode.dev/introduction-to-docker
So, What is Docker Swarm?#
In simple terms, Docker Swarm is a container orchestration tool. It allows you to manage and deploy containers across multiple machines (nodes), treating them as a single virtual Docker host.
Think of Docker Swarm as a way to scale your application and maintain high availability by distributing containers across various systems.
It comes built-in with Docker and is incredibly useful when you want to:
Run containers on multiple hosts
Automatically replicate your containers
Ensure fault tolerance and high availability
Manage services more efficiently
๐ What is Orchestration?#
Orchestration refers to automating the deployment, management, and scaling of containers. When youโre managing dozens or hundreds of containers, orchestration helps you:
Automatically deploy containers
Balance load across nodes
Restart failed containers
Maintain availability even during failures
๐ฏ Key Features of Docker Swarm#
โ
Automated Deployment
โ
Efficient Resource Allocation
โ
Load Balancing & Networking
โ
Service Discovery
โ
High Availability & Fault Tolerance
Letโs say one container crashes โ Swarm detects it and spins up a replica on another available node, keeping your application alive. How it actually works:

As you have two working nodesโone as the manager node and the other as the worker nodeโhere is how they work. The worker node communicates with the manager node using HTTP.

โ๏ธ Setting Up Docker Swarm#
Weโll set this up on two machines:
๐ง zenvilla โ Swarm Manager
โ๏ธ dawood โ Swarm Worker
Note: This can be done using two physical machines or virtual machines on the same network.
๐ง Pre-requisites (Run on both machines)
# Step 1: Install Docker
sudo pacman -Syu docker
# Step 2: Enable and Start Docker
sudo systemctl enable docker
sudo systemctl start docker
# Step 3: Add user to docker group
sudo usermod -aG docker $USER
newgrp docker
๐ Ensure both systems are on the same network#
Check connectivity:
# On zenvilla:
ping <dawood-IP>
# On dawood:
ping <zenvilla-IP>
Also check the ping on Dawood’s laptop.

๐ง On ZENVILLA (Manager Node)#
Step 5: Initialize the Swarm:#
docker swarm init --advertise-addr <ZENVILLA-IP>
Replace <ZENVILLA-IP> with the actual IP of your machine, e.g. 192.168.1.10.
This command will output a token used to join other nodes to the swarm.
โ๏ธ On DAWOOD (Worker Node)#
Step 6: Join the Swarm#
Use the command you received from the manager:
docker swarm join --token <TOKEN> <MANAGER-IP>:2377This command connects dawood to zenvilla as a worker node.

โ Back to ZENVILLA (Verify the Swarm)#
Step 7: Check Node Status#
docker node ls
๐ Deploying a Test Service#
Letโs deploy a lightweight Alpine service to test our setup.
docker service create --name alpine-test \
--replicas 3 \
alpine ping 8.8.8.8
Check if the service is running:
docker service lsTo see where containers are running:
docker service ps alpine-test
On dawood, run:
docker ps
You should see some containers running from the alpine-test service.
โจ Final Thoughts#
Docker Swarm is a powerful yet beginner-friendly way to orchestrate containers. It allows you to:
Run and manage containers across multiple systems
Automatically recover from failures
Scale your services seamlessly
If you’re comfortable with Docker and Docker Compose, exploring Swarm is your next step toward mastering container orchestration. If you liked this guide, feel free to reach out or drop comments for queries. Keep experimenting, and happy containerizing! ๐ณโ๏ธ
P.S.#
If you spot any mistakes, please don’t hesitate to point them out. We’re all here to learn together! ๐
Haris
FAST (NUCES)
BS Computer Science | Class of 2027
๐ GitHub: https://github.com/Zenvila
๐ LinkedIn: https://www.linkedin.com/in/haris-shahzad-7b8746291/
๐ Member: COLAB (Research Lab)
