Build Your First Docker Swarm Cluster

ZenvilaZenvila
4 min read

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.

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:

  • 🧠 zenvillaSwarm Manager

  • ⚒️ dawoodSwarm 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>:2377

This 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 ls

To 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)

0
Subscribe to my newsletter

Read articles from Zenvila directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Zenvila
Zenvila

I'm Haris aka Zen, currently in my 4th semester of Computer Science at FAST-NUCES and a member of COLAB (Research Lab) in Tier 3. I'm currently exploring AI/ML in its early stages, and also focusing on improving my problem-solving techniques. 🐧 Proud user of Arch Linux | Command line is my playground. I'm interested in Automation & Robotics Automation enthusiast on a mission to innovate! 🚀 Passionate about turning manual tasks into automated brilliance.