Before you begin: each host must have Docker Engine installed and reachable over the network. One node will act as the swarm manager; the others will be worker nodes. Use a private network or secure connections for production clusters.

Initialize the Swarm (on the manager)
On the node you designate as the manager, rundocker swarm init. Use --advertise-addr to specify the manager address other nodes should use to join the swarm. The init command prints the docker swarm join command you will run on each worker.
docker swarm join command and run it on each worker node to add them to the cluster.
Why use Swarm vs. running containers manually?
You could manually run containers on each host withdocker run, but doing this at scale creates operational challenges:
- No built-in scheduling or placement logic.
- No automatic load balancing across hosts.
- No automatic restart or self-healing of failed instances.
- Manual, repetitive commands across many hosts.
docker run on separate hosts (illustrative):
Docker Services — the Swarm abstraction
Swarm introduces Docker services. A service declares a desired state for one or more replicas of an application. The manager schedules service tasks (container instances) onto worker nodes, handles restarts, and balances placement across the cluster. Create a service on the manager and specify replicas:docker service ps:
docker service create that you use with docker run: environment variables, published ports, networks, resource limits, and placement constraints.
Common Swarm commands
Best practices and considerations
- Run service management commands (create/scale/update) on manager nodes, not on worker nodes.
- Use overlay networks for multi-host container networking.
- Configure manager quorum and understand leader election for availability.
- Use constraints and placement preferences to control where tasks run (e.g.,
--constraint node.role==worker). - Plan for rolling updates and use
docker service updatewith update parameters to avoid downtime.
Run service management commands (for example
docker service create, docker service ps, docker service scale) on a manager node — not on worker nodes.Further topics to explore
- Manager quorum and automatic failover
- Overlay networks and encrypted traffic between nodes
- Service updates, rollbacks, and healthchecks
- Resource reservations, limits, and placement constraints
- Integrating Swarm with CI/CD pipelines and logging/monitoring tools
Links and references
- Docker Swarm overview — Docker Documentation
- Deploy services to a swarm — Docker Docs
- Overlay networking — Docker Docs