Docker Swarm Setup: Turn a Pile of Hosts into One Cluster
— ny_wk
Docker Swarm takes a handful of separate Docker hosts and makes them behave like one big virtual engine. You deploy a service once, and Swarm decides which machines run the containers, keeps the right number alive, and load-balances across them. If you want orchestration without the full weight of Kubernetes, this is the gentle on-ramp.
What Docker Swarm gives you
- Clustering: several Docker hosts act as a single pool of capacity.
- High availability: if a container or node dies, Swarm reschedules the work elsewhere.
- Scaling: bump a service from 2 replicas to 10 with one command.
- Built-in load balancing: requests are spread across the running replicas.
A Swarm has two roles: manager nodes (which hold the cluster state and schedule work) and worker nodes (which run the containers). Small clusters can start with one manager; production clusters use an odd number (3 or 5) for fault tolerance.
Step 1 — Prepare the hosts
Install Docker on every machine that will join, make sure the Docker service is running, and open the Swarm ports between them: 2377/tcp (cluster management), 7946/tcp+udp (node communication), and 4789/udp (overlay network traffic). Give the nodes network reachability to each other.
Step 2 — Initialize the manager
On the machine you want as the first manager, run:
docker swarm init --advertise-addr <MANAGER_IP>
This creates the Swarm and prints a join command with a token. Keep that output — the workers need it.
Step 3 — Join the worker nodes
On each worker, paste the join command from the previous step:
docker swarm join --token <WORKER_TOKEN> <MANAGER_IP>:2377
Lost the token? On the manager, run docker swarm join-token worker to print it again (or ... manager for a manager token).
Step 4 — Verify the cluster
Back on the manager:
docker node ls
You should see every node listed, with managers marked as Leader/Reachable. An Active status means the node is ready for work.
Step 5 — Deploy a service
Now run something across the cluster:
docker service create --name web --replicas 3 -p 80:80 nginx
Swarm schedules three nginx containers across your nodes and load-balances port 80 to them. Scale anytime with docker service scale web=6, and watch it with docker service ps web.
Key takeaways
- Swarm unifies many Docker hosts into one cluster with managers (brains) and workers (muscle).
docker swarm initstarts it;docker swarm joinadds nodes.- Open ports 2377, 7946, and 4789 between nodes or joining will fail.
- Deploy and scale with
docker service create/docker service scale.
Frequently asked questions
How many manager nodes should I run?
An odd number — 3 or 5 in production — so the cluster keeps a quorum if one manager fails. A single manager is fine for testing.
What if I lose the join token?
Run docker swarm join-token worker (or manager) on a manager to reprint it.
Is Docker Swarm still worth using over Kubernetes?
For small-to-medium deployments that want simple orchestration with native Docker tooling, yes — it is far easier to set up. Kubernetes wins for large, complex, multi-team platforms.
How does Swarm handle a failed node?
The manager notices the node is unreachable and reschedules its containers onto healthy nodes to maintain the desired replica count.
That is the whole loop: init a manager, join your workers, deploy a service. From there, scaling and self-healing come for free.