Docker Swarm

Also known as: swarm mode

On this page 8

Docker Swarm is the native container orchestration mode of Docker Engine that joins several hosts into one cluster, called a swarm, and runs declaratively defined services across them. In the field of containers and cloud infrastructure, it is usually described by its official name, swarm mode, which has been part of the Docker Engine since version 1.12 and replaced the older standalone Docker Swarm product.

Swarm mode keeps the familiar Docker command line and the Compose file format, so teams can move from a single host to a small cluster with minimal new concepts. It provides scheduling, service discovery, load balancing, rolling updates and encrypted node communication without any additional software.

How Docker Swarm organizes a cluster

A swarm consists of manager nodes and worker nodes, all running the same Docker Engine. Managers hold the cluster state and make scheduling decisions, while workers only execute the tasks assigned to them. A manager can also run workloads unless it is drained, which is a common choice in small clusters.

The managers replicate cluster state with the Raft consensus algorithm. Raft requires a majority quorum of managers to accept any change, so a swarm with three managers tolerates one failure and a swarm with five managers tolerates two. If quorum is lost, existing containers keep running, but no new scheduling or updates are possible until quorum is restored.

The table below shows how the number of managers affects fault tolerance.

ManagersQuorum sizeFailures toleratedTypical use
110Development and testing
321Small production clusters
532Larger or critical clusters
743Rarely needed; adds Raft overhead
Manager count and fault tolerance

An odd number of managers is recommended because an even number adds no extra tolerance. Running more than seven managers is discouraged, since every write must be replicated to a majority, which slows consensus without improving resilience.

Services, tasks and the desired state model

The central object in Docker Swarm is the service, a declaration of which image to run, how many copies, which networks and volumes to attach and how updates should proceed. The managers continuously compare this desired state with the actual state and create or remove tasks to close any gap, a process known as reconciliation.

Each task is a single container bound to a node. When a node fails, its tasks are marked as down and the scheduler reschedules replacements on healthy nodes. A stack groups several services defined in one Compose file and deploys them together with a single command.

The following table summarizes the main service modes and objects.

ObjectWhat it representsKey behavior
Replicated serviceA fixed number of identical tasksSpread across available nodes
Global serviceOne task on every eligible nodeSuits agents such as log shippers
TaskOne container assigned to a nodeReplaced, never moved, on failure
StackServices from one Compose fileDeployed and removed as a unit
Secret and configEncrypted or plain data for servicesMounted into containers at runtime
Core objects in swarm mode

Networking and load balancing in swarm mode

Docker Swarm connects services through overlay networks, which use VXLAN encapsulation to give containers on different hosts a shared virtual network. Every service receives a stable virtual IP address and a DNS name, so other services can reach it by name regardless of where its tasks run.

Published ports use the routing mesh, an ingress network that accepts traffic on the published port of every node and forwards it to a healthy task through IPVS-based load balancing. A request can therefore arrive at a node that runs no copy of the service and still be served. Operators who need client source addresses or direct routing can switch to host mode publishing, which bypasses the mesh.

Communication between nodes is protected by mutual TLS with certificates issued by an internal certificate authority on the managers, and certificates rotate automatically. Overlay data traffic is not encrypted by default, but it can be encrypted with IPsec by creating the network with the encrypted option, at a measurable performance cost.

Docker Swarm compared with other orchestrators

Docker Swarm competes with Kubernetes and managed services such as Amazon ECS, and the choice usually turns on operational simplicity versus ecosystem breadth. Swarm is far easier to install and run, while Kubernetes offers more extension points, a larger tool ecosystem and wider support from cloud providers.

The comparison below covers the attributes that most often decide the choice.

AttributeDocker SwarmKubernetesAmazon ECS
Setup effortOne init commandSignificant, often managedManaged by AWS
Configuration formatCompose filesYAML manifests and Helm chartsTask definitions
AutoscalingNot built inHorizontal and cluster autoscalersService and capacity autoscaling
ExtensibilityLimited pluginsCustom resources and operatorsAWS integrations
Ecosystem sizeSmallVery largeAWS-centered
Best fitSmall to medium clustersLarge, complex platformsWorkloads committed to AWS
Swarm mode, Kubernetes and Amazon ECS

Docker Swarm should also be distinguished from Docker Compose. Compose runs a multi-container application on a single host, while swarm mode deploys the same file format across many hosts with scheduling and self-healing.

Operating a swarm in practice

A swarm is created with docker swarm init on the first manager, and other nodes join with a join token that determines whether they become managers or workers. Services are then deployed with docker service create or, more commonly, with docker stack deploy and a Compose file.

Rolling updates are configured per service: the update parallelism, the delay between batches and the failure action decide how quickly a new image reaches production. Setting the failure action to rollback and defining a container health check lets the swarm stop a faulty release automatically.

The decision table below lists typical operational choices.

SituationRecommended settingReason
Production clusterThree or five managersSurvives manager failure
Busy managersDrain managers from workloadsProtects Raft responsiveness
Stateful databasePlacement constraint by node labelKeeps data on the right host
Risky releaseParallelism 1 with automatic rollbackLimits the blast radius
CredentialsSwarm secrets, not environment variablesEncrypted at rest and in transit
Common configuration decisions

Limitations and common mistakes

The main limitation of Docker Swarm is its narrow feature set and slower development pace. It has no built-in autoscaling, a limited storage integration model and few third-party operators, so teams that outgrow it often face a migration to Kubernetes later.

  • Losing manager quorum through an even manager count or by placing all managers in one failure domain.
  • Storing state in local volumes without placement constraints, which leaves data behind when tasks are rescheduled.
  • Assuming overlay traffic is encrypted when the encrypted network option was never enabled.
  • Forgetting to back up the Raft state directory on managers, which is needed to recover a destroyed cluster.
  • Leaving the autolock feature disabled, so stolen manager disks expose Raft encryption keys.

The short version

Docker Swarm lets a group of ordinary servers act like one large computer for running containers. You tell it what you want, for example five copies of a web application, and it decides which machine runs each copy.

If a machine breaks, Swarm notices and starts the missing copies elsewhere. It also spreads incoming visitors across the copies. The key idea is that you describe the result you want, and Swarm keeps the cluster matching that description on its own.

Where it makes a difference

A regional logistics company runs its shipment tracking portal on four virtual machines: three managers that also run light workloads and one larger worker. The team already used Docker Compose in development, so it deploys the same file as a stack with six replicas of the web service, two replicas of an API and a single database pinned to the worker by a node label.

During a routine cloud maintenance event, one manager is restarted without warning. The remaining two managers keep quorum, the scheduler starts replacement tasks within seconds, and the routing mesh sends customer traffic to healthy replicas. Later, a faulty release fails its health check after the first batch, and the configured rollback restores the previous version automatically. The two-person operations team avoided the cost of running a Kubernetes control plane while still getting self-healing and zero-downtime updates.