Docker Swarm Setup Guide¶
A guide for initializing and managing a Docker Swarm cluster for container orchestration across multiple nodes.
Concepts¶
| Term | Description |
|---|---|
| Manager | Nodes that manage the cluster state and schedule services |
| Worker | Nodes that run containerized workloads |
| Service | A definition of tasks to run on the cluster |
| Task | A single container instance running as part of a service |
| Overlay Network | Multi-host networking for Swarm services |
Tip: For high availability, use 3 or 5 manager nodes (always an odd number for Raft consensus).
Initialize Swarm¶
On the First Manager Node¶
This outputs join tokens for managers and workers.
Retrieve Join Tokens¶
Join Nodes¶
Join as Worker¶
Run on each worker node:
Join as Manager¶
Run on additional manager nodes:
Verify Cluster¶
Deploy Services¶
Create a Service¶
List Services¶
Scale a Service¶
Update a Service¶
Remove a Service¶
Stack Deployment (Compose Files)¶
Deploy a Stack¶
Example Stack File¶
# docker-compose.yml
services:
web:
image: nginx:latest
deploy:
replicas: 3
placement:
constraints:
- node.role == worker
restart_policy:
condition: on-failure
ports:
- "80:80"
visualizer:
image: dockersamples/visualizer:latest
deploy:
placement:
constraints:
- node.role == manager
ports:
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
List Stacks¶
Remove a Stack¶
Multi-Manager HA¶
For production, run 3 manager nodes:
# On manager1 (first init)
docker swarm init --advertise-addr 10.0.0.10
# On manager2
docker swarm join --token <manager_token> 10.0.0.10:2377
# On manager3
docker swarm join --token <manager_token> 10.0.0.10:2377
Promote / Demote Nodes¶
# Promote worker to manager
docker node promote <node_name>
# Demote manager to worker
docker node demote <node_name>
Networking¶
Create Overlay Network¶
Use in Service¶
Monitoring and Troubleshooting¶
Node Status¶
Service Logs¶
Drain a Node (Maintenance)¶
# Remove workloads from a node
docker node update --availability drain <node_name>
# Bring node back
docker node update --availability active <node_name>
Leave Swarm¶
Required Ports¶
| Port | Protocol | Description |
|---|---|---|
| 2377 | TCP | Cluster management |
| 7946 | TCP/UDP | Node communication |
| 4789 | UDP | Overlay network traffic |