Skip to main content
Docker Swarm lets you cluster multiple Docker hosts into a single, fault-tolerant orchestration platform. Swarm provides built-in load balancing, service scheduling, and high availability by distributing containerized application replicas across manager and worker nodes.
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.
A minimalist slide showing the words "Docker Swarm" centered on a white background with a small blue-to-teal underline and "Copyright KodeKloud" in the bottom-left corner.

Initialize the Swarm (on the manager)

On the node you designate as the manager, run docker 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.
Copy the 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 with docker 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.
Example of repeatedly running 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:
Inspect where replicas are running with docker service ps:
You can pass many of the same options to 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 update with 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

Watch Video