docker swarm init --advertise-addr 10.0.0.1★Run on the first node — it becomes a manager.--advertise-addris the IP other nodes reach it on. Prints the worker join command.docker swarm join --token SWMTKN-... 10.0.0.1:2377★Run on each other host to join as a worker. Port 2377 = cluster management (TCP).docker swarm join-token manager · workerPrint the token to add more managers or workers. Rotate a leaked token withjoin-token --rotate(card 3).docker swarm leave --force # --force on a managerRemove a node from the swarm.docker node rm <id>from a manager to evict a downed node.
docker node ls★List nodes with role (manager/worker), status, and availability. Run only on a manager.docker node promote <node> · docker node demote <node>Change a node between worker and manager. Keep an odd number of managers (3 or 5) for Raft quorum.docker node update --availability drain <node>★Drain moves tasks off a node (for maintenance).activeto return it;pauseto stop new tasks only.docker node update --label-add zone=eu <node>Add labels used later by placement constraints (card 13).# losing manager quorum = the cluster can't schedulegotchaManagers use Raft; if more than half are down you lose quorum. Spread 3/5 managers across failure domains; keep workers plentiful. Recover from backup orswarm init --force-new-cluster.
docker swarm init --autolock · docker swarm update --autolock=true★hardenAutolock encrypts the Raft logs (which hold secrets/configs) at rest; after a daemon restart the swarm stays locked until you supply the unlock key. Prints the key on enable.docker swarm unlock # paste key after a restart docker swarm unlock-key --rotate # rotate the unlock key★unlockre-enables an autolocked manager;unlock-keyprints (or rotates) the current key. ⚠️ Store the key in a password manager — lose it and a fully-restarted autolocked swarm can't recover.docker swarm join-token --rotate workerInvalidate a leaked join token and issue a new one (existing members stay joined). Do this if a token ever leaks.docker swarm ca --rotate · docker swarm update --cert-expiry 720hRotate the cluster CA and re-issue every node's TLS cert (mutual TLS is automatic). Node certs auto-renew (default 90 days); tune with--cert-expiry.
docker service create --name web --replicas 3 -p 80:80 nginx:alpine★A service = the desired state of a containerized app across the cluster. Swarm schedules--replicastasks and keeps them running.docker service create --mode global --name agent monitoring:latestglobal mode runs exactly one long-running task on every node (log/metrics agents). Default isreplicated.docker service ls · docker service ps web★lsshows services & replica counts;ps <svc>shows each task, its node, and state (running/failed/rejected).docker service logs -f web · docker service rm webAggregate logs across replicas; remove the service.
docker service create --mode replicated-job \ --replicas 10 --max-concurrent 2 --name migrate migrate:latest★jobsA replicated job runs--replicastasks to completion (not forever),--max-concurrentat a time (default = replicas). For batch work: DB migrations, imports, one-off scripts.docker service create --mode global-job --name cleanup cleanup:latestA global job runs exactly one task on every current node, once — e.g. a node-local cleanup/backup pass. No--replicas.docker service ps migrate # tasks show state "Complete", not "Running"Jobs differ from services: a service restarts finished tasks; a job's tasks finish and stay Complete. Re-run byservice update --force.# jobs need Engine 20.10+; older engines reject the *-job modesversionService jobs landed in Docker Engine 20.10. On older daemons use a one-shotreplicatedservice with--restart-condition noneinstead.