Docker Swarm Cheat Sheet

Swarm cluster, node, service, and stack commands for container orchestration on Docker Engine.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Swarm Basics

Initialize and manage a Docker Swarm cluster.

Initialize swarm

Initialize the current engine as a swarm manager.

bashANYswarmcluster
bash
docker swarm init
Notes

Starts the control plane and prints worker join instructions.

Initialize swarm with advertise address

Initialize swarm and advertise a specific manager IP.

bashANYswarmclusternetwork
bash
docker swarm init --advertise-addr 10.0.0.10
Notes

Important on hosts with multiple interfaces.

Show worker join token

Show or rotate the worker join token.

bashANYswarmtokens
bash
docker swarm join-token worker
Notes

Use the printed command on worker nodes.

Show manager join token

Show or rotate the manager join token.

bashANYswarmtokens
bash
docker swarm join-token manager
Notes

Use carefully since managers have control plane privileges.

Join a swarm

Join a node to an existing swarm.

bashANYswarmcluster
bash
docker swarm join --token <token> 10.0.0.10:2377
Notes

Run this on a worker or manager being added to the cluster.

Leave the swarm

Leave the current swarm.

bashANYswarmclustercleanup
bash
docker swarm leave
Notes

Use `--force` carefully on a manager.

List swarm nodes

List nodes in the swarm.

bashANYswarmnodes
bash
docker node ls
Notes

Shows manager status, availability, and health.

Inspect a node

Show low-level swarm node details.

bashANYswarmnodesinspect
bash
docker node inspect self
Notes

Useful for labels, availability, and TLS info.

Add node label

Add or update node labels.

bashANYswarmnodeslabels
bash
docker node update --label-add zone=us-east-1a worker-1
Notes

Labels are useful for placement constraints.

Drain a node

Mark a node unavailable for task scheduling.

bashANYswarmnodesmaintenance
bash
docker node update --availability drain worker-1
Notes

Useful before maintenance or draining workloads.

Swarm Services

Create, inspect, scale, and update services in a swarm.

Create a service

Create a replicated or global service in the swarm.

bashANYswarmservices
bash
docker service create --name web -p 8080:80 nginx:latest
Notes

This is the main swarm service deployment primitive.

List services

List swarm services.

bashANYswarmservices
bash
docker service ls
Notes

Shows replica state and image references.

List service tasks

List the tasks for a service.

bashANYswarmservicestasks
bash
docker service ps web
Notes

Useful for troubleshooting placement and task failures.

Inspect a service

Show low-level service details.

bashANYswarmservicesinspect
bash
docker service inspect web
Notes

Inspect update config, rollback policy, labels, and networking.

Scale a service

Scale a service to the desired replica count.

bashANYswarmservicesscale
bash
docker service scale web=5
Notes

Can scale multiple services in one command too.

Update service image

Roll out a new image for a service.

bashANYswarmservicesupdate
bash
docker service update --image nginx:1.27 web
Notes

Swarm performs a rolling update based on service update settings.

Update service env

Update service environment variables.

bashANYswarmservicesupdate
bash
docker service update --env-add LOG_LEVEL=debug web
Notes

Useful for changing config without redefining the service from scratch.

Publish a new port

Add a published port to an existing service.

bashANYswarmservicesnetwork
bash
docker service update --publish-add 8443:443 web
Notes

Useful for incremental service exposure changes.

Rollback a service

Rollback a service to the previous spec.

bashANYswarmservicesrollback
bash
docker service rollback web
Notes

Useful after a failed rolling update.

Remove a service

Remove a swarm service.

bashANYswarmservicescleanup
bash
docker service rm web
Notes

Stops and cleans up all tasks of that service.

Deploy a stack

Deploy or update a swarm stack from a Compose file.

bashANYswarmstack
bash
docker stack deploy -c compose.yaml mystack
Notes

A convenient higher-level workflow for multi-service swarm apps.

List stacks

List deployed stacks.

bashANYswarmstack
bash
docker stack ls
Notes

Useful for multi-application swarm clusters.

List services in stack

List services belonging to a stack.

bashANYswarmstack
bash
docker stack services mystack
Notes

Helpful for per-application troubleshooting.

List stack tasks

List tasks for services in a stack.

bashANYswarmstacktasks
bash
docker stack ps mystack
Notes

Useful when diagnosing stack rollout issues.

Remove a stack

Remove a deployed stack.

bashANYswarmstackcleanup
bash
docker stack rm mystack
Notes

Stops and removes the stack's services and networks.