Q: What is Docker Swarm and how does it compare to Kubernetes?
Docker Swarm is Docker Engine's built-in clustering and orchestration mode: you join several hosts into one swarm, declare services, and Swarm schedules, load-balances and restarts the containers.
Want to master this scenario in a live sandbox? KodeKloud's Docker Certified Associate (DCA) Hands-On Lab Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: <strong>Docker Swarm</strong> is Docker Engine's built-in clustering and orchestration mode: you join several hosts into one swarm, declare services, and Swarm schedules, load-balances and restarts the containers. Compared with Kubernetes it is much simpler to set up and uses the familiar Docker CLI and Compose files, but it has a smaller feature set and ecosystem. Interviewers ask this to check that you can compare orchestrators on facts such as scheduling, networking, extensibility and operations, rather than repeating that Kubernetes has won.
Mental model: what Docker Swarm actually is
Swarm mode is a feature of the Docker daemon, not a separate product to install.
- Managers and workers: Manager nodes hold cluster state in a Raft log and schedule tasks; worker nodes only run tasks. Use 3 or 5 managers (an odd number) for quorum.
- Service and task: A service is the desired state (image, replicas, ports); each replica is a task that runs in one container on one node.
- Overlay networking and routing mesh: Overlay networks span nodes, and published ports are reachable on every node through the ingress routing mesh.
- Built-in secrets:
docker secretstores encrypted data in the Raft log and mounts it as files under/run/secretsin the container. - Scheduling: The default strategy spreads tasks across nodes by load; you steer it with placement constraints such as
node.labels.disk==ssdand preferences, but there is no equivalent of Kubernetes affinity, taints or priority classes.
Hands-on: Docker Swarm cluster, service and stack
Create a cluster, deploy a replicated service and then a stack from a Compose file.
docker swarm init --advertise-addr 10.0.0.10
docker node ls
docker service create --name web --replicas 3 -p 80:80 nginx:1.27
docker service ls
docker service ps web # which node runs which task
# compose.yaml (stack)
# services:
# web:
# image: nginx:1.27
# ports: ["80:80"]
# deploy:
# replicas: 3
# update_config: {parallelism: 1, delay: 10s, order: start-first}
docker stack deploy -c compose.yaml mystack
- Initialise:
docker swarm init --advertise-addr <manager-ip>prints a join token for workers. - Join nodes: Run the printed
docker swarm joincommand on each worker, then checkdocker node ls. - Create a service:
docker service create --replicas 3 -p 80:80 nginx:1.27schedules three tasks across the nodes. - Deploy a stack:
docker stack deploy -c compose.yaml mystackuses the Compose file format with adeploy:section for replicas and update policy. - Day-two commands:
docker service update --image nginx:1.27.1 webperforms a rolling update,docker service logs webaggregates task logs, anddocker node update --availability drain <node>evacuates a node for maintenance.
Docker Swarm vs Kubernetes: the real differences
Compare on capabilities that matter in operations rather than popularity.
- Setup: Swarm is one command per node. Kubernetes needs a control plane, a CNI plugin, an ingress controller and usually a managed service.
- Scaling and autoscaling: Swarm scales manually with
docker service scale; Kubernetes has HPA, VPA and cluster autoscalers. - Extensibility: Kubernetes has CRDs, operators, admission webhooks, RBAC and NetworkPolicy. Swarm has no equivalent extension model.
- Rollouts: Swarm supports rolling updates and rollback via
docker service update --rollback; Kubernetes adds probes-driven rollouts and progressive delivery tooling. - Ecosystem: Managed offerings, Helm charts, and vendor support target Kubernetes almost exclusively. Networking model: Swarm gives each service a virtual IP with built-in DNS load balancing; Kubernetes separates Service, Ingress or Gateway API and NetworkPolicy, which is more flexible but more to learn.
Operational caveats and failure modes
Know the places Swarm bites in production.
docker node ls # managers show Leader / Reachable
docker node update --availability drain worker-2 # evacuate before maintenance
docker service update --rollback web # revert last service update
docker service inspect --pretty web | grep -A3 UpdateConfig
# Last resort on a surviving manager after quorum loss:
docker swarm init --force-new-cluster --advertise-addr 10.0.0.10
- Quorum loss: If a majority of managers fail, you cannot change cluster state until quorum returns; recover with
docker swarm init --force-new-clusteron a surviving manager only as a last resort. - Routing mesh surprises: Client source IPs are NATed unless you use host-mode publishing; this affects logging and allow-lists.
- Health checks: Define
HEALTHCHECKor a service health check; otherwise a running but hung process counts as healthy. - Stateful data: Volumes are local to a node by default, so pin stateful tasks with placement constraints or use a volume plugin.
- Upgrades: Upgrade managers one at a time and confirm
docker node lsshows themReachablebefore touching the next one, otherwise you risk losing quorum during the upgrade.
When to use which
The decision is mostly about team size, features required and ecosystem.
- Choose Swarm: A small team, a handful of stateless services, existing Compose files, and no need for operators or autoscaling.
- Choose Kubernetes: Multi-team platforms, autoscaling, policy and multi-tenancy, or any dependency on cloud-native tooling.
- Migration path: Compose files can be converted as a starting point (for example with Kompose), but expect to rewrite networking and secrets.
- Check support status: Swarm mode ships with Docker Engine, but confirm current maintenance and roadmap before betting a new platform on it.
- Observability: Both work with Prometheus and Grafana, but Kubernetes has far more ready-made exporters, dashboards and operators; account for that when estimating effort.
- Docker Swarm is Docker Engine's native clustering mode where managers keep Raft state and schedule replicated services across workers.
- I verify with docker node ls, docker service ps and checking that three or five managers keep quorum and the required ports are open.
- I automate with stack files in Git and rolling update settings, and I would pick Kubernetes once autoscaling, policy or operators are required.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Running two managers Raft needs a majority, so two managers tolerate zero failures; use three or five.
- Opening the Docker API on TCP without TLS It gives root-equivalent control of the host to anyone who can reach it.
- Assuming volumes follow tasks between nodes Local volumes stay on one node, so a rescheduled task starts with empty data.
- Declaring Swarm dead or Kubernetes always better The correct answer depends on scale, team skills and required features.