On this page
- What does a Swarm need before you start?
- Which ports must the nodes reach, and how do you limit them?
- How do you initialise the manager and join workers?
- How many managers should a cluster have?
- How do you create an encrypted overlay network?
- How do you deploy services and stacks?
- How do secrets and persistent data work in Swarm?
- How do you update, drain and remove nodes?
- What breaks most often, and how do you check it?
- Questions
What does a Swarm need before you start?
A Swarm needs two or more servers running the same major Docker Engine release, each with its own reachable IPv4 address and a unique hostname. Our instances come with one dedicated IPv4 and IPv6 each but no private network between them, so every node will talk to the others over its public address. That is perfectly workable as long as the firewall only admits the other nodes and the data plane is encrypted.
Install Docker from the official repository on each machine using the Docker Engine tutorial, or deploy the ready Docker image on Ubuntu 24.04. Then confirm versions match:
docker version --format '{{.Server.Version}}'
hostnamectl hostnameFor this guide the three nodes are node1 at 198.51.100.24 (manager), node2 at 198.51.100.25 and node3 at 198.51.100.26. Give each a distinct name with sudo hostnamectl set-hostname node2 before joining, because Swarm shows hostnames in its node list and duplicates make the output hard to read.
Which ports must the nodes reach, and how do you limit them?
Swarm uses four kinds of traffic between nodes: TCP 2377 for cluster management (only managers listen), TCP and UDP 7946 for node discovery and gossip, UDP 4789 for VXLAN overlay data, and IP protocol 50 (ESP) when overlays are encrypted. None of these should be open to the whole internet. With ufw on Ubuntu or Debian, add one block per peer address on every node:
for peer in 198.51.100.24 198.51.100.25 198.51.100.26; do
sudo ufw allow from $peer to any port 2377 proto tcp
sudo ufw allow from $peer to any port 7946 proto tcp
sudo ufw allow from $peer to any port 7946 proto udp
sudo ufw allow from $peer to any port 4789 proto udp
sudo ufw allow proto esp from $peer to any
done
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status numberedAdding a rule for a node own address is harmless, so one loop can run unchanged everywhere. Remember that ports published by Docker services are inserted into iptables by Docker itself and skip the ufw input rules, so anything you publish with -p or a stack ports: entry is reachable from outside. Keep that in mind when deciding what to publish, and read the firewall tutorial for the general approach on our servers.
How do you initialise the manager and join workers?
Run docker swarm init on the first node and tell it which address to advertise, otherwise Docker may pick the wrong interface or an IPv6 address:
sudo docker swarm init --advertise-addr 198.51.100.24The output contains a ready join command with a worker token. Print it again at any time, and run it on the other two servers:
# on node1
sudo docker swarm join-token worker
# on node2 and node3
sudo docker swarm join --advertise-addr 198.51.100.25 --token SWMTKN-1-xxxx 198.51.100.24:2377Use each joining node own address in --advertise-addr. Back on the manager, list the cluster:
sudo docker node ls
sudo docker info --format '{{.Swarm.LocalNodeState}} {{.Swarm.Nodes}} nodes'All three should show Ready and Active. Treat join tokens like passwords; if one leaks, replace it with sudo docker swarm join-token --rotate worker.
How many managers should a cluster have?
Use one manager for a test cluster and three or five for anything you depend on. Managers keep the cluster state in a Raft store and need a majority to make changes, so three managers survive the loss of one and five survive the loss of two. An even number adds no extra tolerance. Promote workers once they are joined:
sudo docker node promote node2 node3
sudo docker node lsManagers also run normal tasks by default. On small plans that is fine, but if a manager is struggling for memory you can keep workloads off it with sudo docker node update --availability drain node1. To protect the stored keys when a manager restarts, turn on autolock and keep the unlock key somewhere safe: sudo docker swarm update --autolock=true. After a reboot you then run sudo docker swarm unlock on that manager.
How do you create an encrypted overlay network?
Pass --opt encrypted when creating the overlay. Swarm management traffic already uses mutual TLS, but container to container traffic on an overlay travels as plain VXLAN unless you ask for encryption, which matters a great deal when the path between nodes is the public internet.
sudo docker network create \
--driver overlay \
--opt encrypted \
--attachable \
appnet
sudo docker network inspect appnet --format '{{.Options}}'The inspect output should include encrypted. Encryption uses IPsec in the kernel, which is why ESP has to pass the firewall. Expect a small CPU cost on busy links. The --attachable flag lets standalone containers join the network, which is useful for debugging with a throwaway shell.
How do you deploy services and stacks?
A single service is one command, and a stack is a Compose file deployed with docker stack deploy. Start with a quick test service:
sudo docker service create --name web --replicas 3 \
--network appnet --publish 8080:80 nginx:stable
sudo docker service ps web
curl -I http://198.51.100.26:8080The routing mesh answers on port 8080 of every node, even one that is not running a replica. For real applications write a stack file; it is the same format explained in the Docker Compose guide plus a deploy block:
services:
api:
image: ghcr.io/example/api:2.3
networks: [appnet]
deploy:
replicas: 2
update_config:
parallelism: 1
order: start-first
restart_policy:
condition: on-failure
networks:
appnet:
external: truesudo docker stack deploy -c stack.yaml shop
sudo docker stack services shopImages must come from a registry every node can pull from, because Swarm schedules tasks on any node. For a private registry add --with-registry-auth to the deploy command.
How do secrets and persistent data work in Swarm?
Store passwords as Swarm secrets, which are encrypted in the Raft store and mounted as files under /run/secrets inside the container:
printf '%s' 's3cret-value' | sudo docker secret create db_password -
sudo docker secret lsVolumes are local to each node, so a database that keeps data on disk must stay on the same machine. Pin it with a placement constraint such as node.hostname == node1 in the deploy.placement.constraints list. Stateless web and API services are the ones that benefit from moving freely between nodes.
How do you update, drain and remove nodes?
Drain a node before maintenance so its tasks move elsewhere, then set it active again afterwards:
sudo docker node update --availability drain node3
sudo apt update && sudo apt upgrade
sudo docker node update --availability active node3Roll out a new image with sudo docker service update --image ghcr.io/example/api:2.4 shop_api; Swarm replaces replicas one at a time according to update_config, and --rollback returns to the previous spec. To retire a worker, run sudo docker swarm leave on it and sudo docker node rm node3 on a manager. Demote a manager before it leaves so quorum is not affected.
What breaks most often, and how do you check it?
Most Swarm trouble comes from blocked ports or a wrong advertise address. If docker node ls shows a node as Down, test reachability from another node with nc -zv 198.51.100.24 2377 and check sudo ufw status on both sides. If services start but cannot reach each other across hosts, UDP 4789 or ESP is usually blocked. Look at sudo journalctl -u docker --since "15 min ago" for gossip or VXLAN errors, and compare docker info on each node to make sure they advertise public addresses that the others can reach. Clock drift can also upset TLS between nodes, so keep systemd-timesyncd or chrony running.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Docker Swarm across servers
Can Swarm nodes sit in different cities?
Yes, as long as they can reach each other on the listed ports. Latency between managers slows cluster decisions, so keep the managers close together and put workers wherever your users are.
Do I need a private network for Swarm?
No. Swarm works over public addresses. Limit the cluster ports to peer IPs with the firewall inside each server and use --opt encrypted overlays so data between nodes is protected in transit.
Is Swarm still maintained?
Swarm mode is part of Docker Engine (the SwarmKit project) and ships with every current release. It is smaller in scope than Kubernetes, which is often an advantage on a handful of servers.
Why is my published port reachable even though ufw denies it?
Docker writes its own iptables rules for published ports, and those rules run before ufw input rules. Publish only what should be public, or bind services to loopback and place a reverse proxy before the app.
How do I back up a Swarm?
Back up /var/lib/docker/swarm on a manager while Docker is stopped, together with your stack files and application volumes. The optional daily backups add-on also captures the whole disk of each node.
More in Containers and PaaS
- Docker Compose stacks
- Docker volumes and bind mounts
- Read and limit Docker logs
- Free disk space used by Docker
- Build, tag and push Docker images
- Run and manage containers
- Podman and rootless containers
- Automatic updates with Watchtower
- Portainer CE web UI
- Dokploy self-hosted PaaS
- CapRover PaaS
- Dokku git push deploys
- Coolify on your own server