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.docker swarm join-token manager · workerPrint the token to add more managers or workers. Rotate a leaked token withjoin-token --rotate.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 9).# 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.
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 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 scale web=5★Change replica count (orservice update --replicas 5). Swarm converges to the new desired state.docker service update --image nginx:1.27 web★Roll out a new image — Swarm updates tasks per the rolling-update policy (card 10).docker service update --env-add KEY=val --publish-add 443:443 webAdd/remove env, ports, mounts, labels, limits with--*-add/--*-rmflags.docker service rollback web★Revert to the previous spec if an update goes wrong.