Docker Swarm
In short
Docker Swarm is the clustering mode built into Docker Engine: it joins several machines into one swarm and runs containers across them as replicated services.
What is Docker Swarm?
Docker Swarm, officially swarm mode, turns a group of machines running Docker into a single cluster called a swarm. It has been built into Docker Engine since version 1.12 in 2016, so there is nothing extra to install: docker swarm init makes one machine a manager, and other machines join it with a token. It replaced an older standalone tool, now called Classic Swarm, which is no longer developed.
Instead of starting containers one by one, you declare services: which image to run, how many replicas and which ports to publish. Manager nodes store the cluster state and agree on it with the Raft consensus algorithm; they schedule the replicas onto worker nodes and replace any that fail. Swarm also provides overlay networks between machines, a routing mesh that lets any node accept traffic for a published port, rolling updates and encrypted secrets. A whole application can be deployed from a Compose file with docker stack deploy.
If Docker Compose is a recipe cooked in one kitchen, Swarm is the same recipe shared across several kitchens, with a head chef who moves the work elsewhere when one kitchen closes. It suits teams that want clustering with the tools and files they already know, such as small and medium production setups, on-premises servers and edge locations. Mirantis, which bought Docker's enterprise business in 2019, sells commercial support, and swarm mode remains part of the open-source Moby project behind Docker Engine.
Docker Swarm is most often compared with Kubernetes. Both schedule containers across a cluster, restart failed ones and roll out updates, but Swarm keeps to a small set of concepts and reuses the Docker command line and Compose files. Kubernetes has many more features, such as autoscaling, custom resources and operators, and a far larger ecosystem, with managed services from every major cloud. Swarm is easier to learn and run; Kubernetes is the usual choice for large or complex systems.
Key takeaways
- Docker Swarm, or swarm mode, joins several Docker hosts into one cluster.
- It has been built into Docker Engine since 2016; Classic Swarm was a separate, retired tool.
- Managers keep the cluster state with Raft; workers run the containers.
- Services declare the image, replicas and ports; stacks deploy whole Compose files.
- It is simpler than Kubernetes but has a much smaller ecosystem.
Example
# On the first machine: create a swarm; this node becomes a manager
docker swarm init --advertise-addr 10.0.0.1
# On every other machine: join with the token that init printed
docker swarm join --token <worker-token> 10.0.0.1:2377
# Run three replicas of a web app spread across the nodes
docker service create --name web --replicas 3 --publish 80:8080 example/web:1.0
docker service ls
# Roll out a new version, one replica at a time by default
docker service update --image example/web:1.1 web
# Or deploy a whole application from a Compose file
docker stack deploy -c compose.yaml shopReaders ask
Is Docker Swarm still maintained?
Yes. Swarm mode is part of Docker Engine and the open-source Moby project, and it still receives updates. What was retired is Classic Swarm, the separate tool that came before it. Its community and tooling are much smaller than those of Kubernetes, though.
What is the difference between Docker Swarm and Kubernetes?
Both run containers across a cluster. Swarm is built into Docker, uses Compose files and takes minutes to set up; Kubernetes is more complex but offers far more features, extensions and managed cloud services.
What is the difference between Docker Compose and Docker Swarm?
Compose runs a multi-container application on one machine. Swarm runs services across several machines with replicas, failover and rolling updates, and it can deploy the same Compose file as a stack.
See also
- KubernetesDevOps & Cloud, p. 37Kubernetes is an open-source system that automates deploying, scaling, and managing containerized applications across a cluster of machines.
- DockerDevOps & Cloud, p. 20Docker is an open-source platform for packaging an application and everything it needs into a container that runs the same way on any machine.
- Docker ComposeDevOps & Cloud, p. 21Docker Compose is a tool for defining and running multi-container applications, such as a web server plus a database, from one YAML file with one command.
- ContainerDevOps & Cloud, p. 13A container is a lightweight, isolated package that bundles an application with its dependencies and runs it on the host's shared operating system kernel.
- Consensus AlgorithmSoftware Architecture, p. 9A consensus algorithm lets a group of machines agree on a single value or an ordered log of decisions, even when some of them crash or messages are lost.
- Load BalancerDevOps & Cloud, p. 40A load balancer is a server or service that spreads incoming traffic across several backend servers so no single one is overloaded and the app stays available.
Sources
Spotted a mistake or something missing on this page?Suggest an edit