In this lesson, you will learn how to transform isolated Docker hosts into a unified, decentralized cluster using Docker Swarm mode.
Up until now, you have managed containers on a single host. In a production environment, this is risky; if that single machine fails, your application goes down. Docker Swarm solves this by allowing multiple machines to work together as a single virtual "swarm."
The Manager and the Workers
A Swarm cluster consists of two types of nodes:
- Manager Nodes: These act as the "brain" of the cluster. They handle cluster management tasks, maintain the state of the swarm, and schedule services.
- Worker Nodes: These are the "muscle." Their sole job is to execute the containers assigned to them by the manager.
Initializing the Cluster
To start a cluster, you must first initialize it on your chosen manager node. Run the following command, replacing <MANAGER-IP> with the actual IP address of that machine:
docker swarm init --advertise-addr <MANAGER-IP>
Once initialized, Docker provides a specific join token. To add other machines as workers, you simply execute the provided docker swarm join command on those machines:
docker swarm join --token <TOKEN> <MANAGER-IP>:2377
Why this matters
By spreading your containers across multiple nodes, you move away from a single point of failure. If one worker node goes offline, the manager detects the loss and automatically reschedules those containers onto the remaining healthy nodes in the cluster. This decentralized approach ensures that your services remain available even when individual physical or virtual servers experience hardware issues or network interruptions.
You can verify your cluster is active and see your connected nodes at any time by running docker node ls on the manager node. This will display all nodes currently participating in the swarm and their roles.
Key Takeaway: Initializing a Swarm cluster enables decentralized orchestration by designating manager nodes to oversee cluster state while worker nodes provide the compute power to host your resilient, multi-node applications.