

Sarthak Varshney is a Docker Captain, 5x C# Corner MVP, and 2x Alibaba Cloud MVP, with over six years of hands-on experience in the IT industry, specializing in cloud computing, DevOps, and modern application infrastructure. He is an Author and Associate Consultant, known for working extensively with cloud platforms and container-based technologies in real-world environments.
So here's a question I got from a student last week during one of my university sessions: "Sir, I know how to run a container. I know docker run, I know Compose. Why do I need Swarm at all?"
Fair question. Let me answer it the way I'd answer it over chai, not the way a textbook would.
Imagine you've built a small app — let's say it's an online quiz platform for a college fest. You containerized it, you ran docker run on your laptop, it worked beautifully. Demo day went great. Everyone clapped.
Now fast forward two weeks. The same platform needs to handle 5,000 students hitting it at once during the actual event registration window. Your single container, running on your single machine, is going to fall over. Not might — will. One container, one server, one point of failure. If that machine reboots, restarts, or just gets overwhelmed, your app is down and there's nobody around at 2 AM to type docker start again.
This is exactly the gap that container orchestration exists to fill. And Docker Swarm is Docker's own answer to that gap.
I want you to forget the word "orchestration" for a second, because it sounds intimidating, like something only senior engineers with fifteen years of experience get to touch. It isn't. Strip away the jargon and orchestration just means: automatically managing where your containers run, how many of them run, what happens when one dies, and how traffic finds them.
That's it. Four things:
You could technically do all four of these manually. SSH into five servers, run containers by hand, watch them, restart the dead ones, update a load balancer config every time something moves. People genuinely did this in the early Docker days. It was miserable. Orchestration tools like Swarm and Kubernetes exist because humans are bad at doing repetitive infrastructure babysitting at 3 AM, and computers are great at it.
Here's the analogy I use in my sessions, and I promise it's not a lazy pun just because the word "orchestration" contains "orchestra."
Think of a live orchestra performance. You've got a violin section, a cello section, brass, percussion — dozens of musicians. Each one is skilled and can technically play on their own. But if you just put fifty talented musicians in a room and told them "please play Beethoven's 5th, good luck," you'd get chaos. Beautiful, talented chaos, but chaos.
What makes it music instead of noise is the conductor. The conductor doesn't play a single instrument. Their entire job is coordination — telling the violins when to come in, telling percussion to hold back, watching the whole stage and adjusting tempo, and if a musician suddenly can't continue, quietly cueing someone else to cover.
Now map that to Docker Swarm:
If one violinist gets a stomach ache and has to step out mid-performance, the conductor doesn't stop the show. Someone else in that section picks up the slack, and the audience barely notices. That's exactly what Swarm does when a container crashes — it notices, and it launches a replacement on its own, without you lifting a finger.
This is the mental model I want you to carry through the rest of this article. Every technical term below maps back to this orchestra.
In Swarm terms, every machine — physical or virtual — that's part of your cluster is called a node. A node could be your laptop, a VM on AWS, a bare-metal server in a data center, doesn't matter. If it's got Docker installed and it's joined the swarm, it's a node.
Think of nodes as the actual physical space on stage. You could have a tiny stage with just three musicians, or a massive concert hall with two hundred. The stage size (number of nodes) determines how big a "performance" (workload) you can pull off.
There are two kinds of nodes in a swarm, and this distinction is the whole ballgame.
A manager node is the one making decisions. It maintains the cluster state, decides where containers should be scheduled, handles the API requests you send it (docker service create, docker service scale, and so on), and keeps track of who's healthy and who's not.
You can have multiple managers for high availability — using something called Raft consensus under the hood, which is a fancy way of saying "the managers vote amongst themselves so that if one manager dies, the others already agree on the current state and can carry on." Odd numbers are recommended for managers (1, 3, or 5) specifically because voting needs a majority, and you don't want ties.
A worker node just does what it's told. It runs the containers assigned to it by the manager. It doesn't make scheduling decisions, it doesn't vote, it just executes.
Here's something that trips up a lot of students: a manager node can also run containers by default, meaning it's technically both conductor and musician at the same time. In small setups, that's fine. In production, you'll often see people "drain" the manager nodes so they only manage and don't run application workloads, keeping the conductor focused purely on conducting.
Enough theory. Let's actually do this. I'm assuming you've already got Docker installed — if not, go install Docker Desktop or Docker Engine first, this article assumes that part is done.
On the machine you want to be your first manager, run:
docker swarm init
If you're on a machine with multiple network interfaces (common on cloud VMs), Docker might complain and ask you to specify which IP address to advertise. In that case:
docker swarm init --advertise-addr <YOUR-IP>
Once this runs, Docker will spit out a message that looks something like this:
Swarm initialized: current node (abc123xyz) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-xxxxxxx 192.168.1.10:2377
Copy that docker swarm join command somewhere safe. That token is basically your invitation letter to the orchestra — anyone with that token can join your swarm as a worker.
If you've got a second machine or VM lying around (a lot of students spin up a couple of free-tier cloud VMs just for practice, which I'd honestly recommend), go to that machine and paste in the join command you copied:
docker swarm join --token SWMTKN-1-xxxxxxx 192.168.1.10:2377
If you only have one laptop, that's fine too — a single-node swarm is a completely valid way to learn the concepts. Everything I'm about to show you works the same way, just with all containers landing on the one node you've got.
Back on the manager, run:
docker node ls
You'll see a table listing every node in your swarm, whether it's a Leader (the primary manager), a Manager, or a Worker, and whether it's Ready and Active. This is your seating chart — literally showing you who's on stage and what role they're playing.
Here's where the terminology shifts slightly, and it's important. In plain Docker, you run containers. In Swarm, you run services. A service is a declaration of intent — "I want N copies of this image running, always." Swarm's job is to make reality match that declaration, continuously, forever, without you asking twice.
Let's create one:
docker service create --name my-web --replicas 3 -p 8080:80 nginx
Let's break this down piece by piece, because every flag here matters:
--name my-web — just naming the service so you can refer to it later--replicas 3 — this is the big one. You're telling Swarm "I always want 3 copies of this running." Not "run it once." Always maintain three.-p 8080:80 — publish port 8080 on any node, routed to port 80 inside the containersnginx — the image to useNow check what happened:
docker service ls
You'll see your service listed, with something like 3/3 under replicas, meaning all three desired copies are up and running.
Want to see exactly where those three copies landed?
docker service ps my-web
This shows you which node each replica is running on. If you've got multiple worker nodes, Swarm will have spread these three copies across them automatically — that's the scheduler doing its conductor job, deciding placement so you're not overloading one machine.
Now for the fun part — the whole reason we're doing this. Go find one of those three containers and kill it manually, just to see what happens:
docker ps
docker stop <container-id-of-one-replica>
Wait a few seconds, then run docker service ps my-web again. You'll see the stopped one marked as failed, and — without you doing anything — a brand new replica already spinning up to replace it. That's your conductor noticing a musician stepped off stage, and quietly cueing a replacement. This self-healing behavior is the single biggest reason orchestration exists.
Say the quiz platform I mentioned earlier suddenly needs more capacity because registration just opened. You don't tear anything down and rebuild. You just tell Swarm the new desired state:
docker service scale my-web=6
Swarm goes from 3 replicas to 6, again spreading them across your available nodes. Traffic hitting your published port gets automatically load-balanced across however many replicas currently exist, thanks to Swarm's built-in routing mesh.
Mistake 1: Confusing docker run with docker service create. Students often try to docker run inside a swarm setup expecting orchestration benefits. It won't self-heal, it won't scale, it won't spread across nodes. If you want Swarm's superpowers, you must use docker service create, not plain docker run.
Mistake 2: Forgetting the port publish flag and then wondering why nothing loads in the browser. If you don't add -p, your service has no way for the outside world to reach it.
Mistake 3: Having an even number of manager nodes. I mentioned this earlier but it's worth repeating because I see it constantly — people spin up 2 or 4 managers thinking "more is better" and then get confused when a network split causes the cluster to lose quorum. Stick to 1, 3, or 5 managers.
Mistake 4: Treating the manager node like it's special hardware. It's not about CPU power — the manager needs to be reliably reachable on the network more than it needs to be beefy. A weak but stable manager beats a powerful but flaky one.
Mistake 5: Panicking when docker service ps shows old failed tasks listed. Swarm keeps a short history of past task attempts for your service by default so you can debug what went wrong. Seeing old "Shutdown" entries isn't an error — it's a log, not a current problem.
Don't just read this and move on — actually get your hands dirty, because Swarm concepts only click once you've watched them happen live. Here's your challenge:
nginx with 4 replicas, publishing it on port 8080.http://localhost:8080 and confirm you see the nginx welcome page.docker stop and watch Swarm bring it back within seconds using docker service ps.docker service rm my-web and leave the swarm with docker swarm leave --force to clean up.Once you've done this once with your own hands, the whole "orchestra" analogy will stop being an analogy and start being something you've actually watched happen on your own screen. That's the moment Swarm stops feeling like theory and starts feeling like a tool you understand.
This is Part of the ongoing Docker Zero to Hero series — next up, we'll look at Docker Swarm networking in depth, including overlay networks and how the routing mesh actually moves traffic between nodes behind the scenes.