Docker Swarm Cheat Sheet
September 11, 2023 • 5 minutes read

Docker Swarm Cheat Sheet

Manage Docker Swarm Nodes

List nodes in the swarm

docker node ls

Display detailed information about node(s)

docker node inspect node-unique-id --pretty

List tasks running on current node

docker node ps

List tasks running on remote node(s)

docker node ps node-unique-id

Remove one or more nodes from the swarm

docker node rm node-unique-id

Promote one or more nodes to manager in the swarm

docker node promote node-unique-id

Demote one or more nodes from manager in the swarm

docker node demote node-unique-id

Drain (relocate all tasks) a node that had a task assigned to it

docker node update --availability drain node-unique-id

Return the drained node to an active state

docker node update --availability active node-unique-id

Change node availability to pause state. Pause means the scheduler doesn't assign new tasks to the node, but existing tasks remain running

docker node update --availability pause node-unique-id

Manage Docker Swarm Services

List services in the swarm

docker service ls

Display the details about a service

docker service inspect --pretty docker-cluster-name_nginx

See which nodes are running the service

docker service ps docker-cluster-name_nginx

Scale the service in the swarm. Supports up scale / down scale by setting number of tasks (containers)

docker service scale docker-cluster-name_nginx=8

Delete the service running on the swarm

docker service rm docker-cluster-name_nginx

Commands inside container in Docker Swarm

Run command ("php -v" in example below) inside docker container in the swarm

docker exec -it `docker ps -f 'name=docker-cluster-name_php' -q --no-trunc | head -n1` php -v

Show environment variables of running container in the swarm

docker exec -it `docker ps -f 'name=docker-cluster-name_php' -q --no-trunc | head -n1` printenv

Run interactive shell inside container

docker exec -it `docker ps -f 'name=docker-cluster-name_php' -q --no-trunc | head -n1` sh

Manage Logs in Docker Swarm

Show the last 5 minutes logs from the nginx service

docker service logs docker-cluster-name_nginx --since 5m

Show logs in real time from the nginx service since 1 minute ago

docker service logs -f docker-cluster-name_nginx --since=1m

Show logs in real time from the nginx service since now

docker service logs -f docker-cluster-name_nginx --since=0s

Display logs in real time from the nginx service filtered by string "ELB-HealthChecker"

docker service logs -f docker-cluster-name_nginx | grep -w "ELB-HealthChecker"

Display logs from the nginx service filtered by 302 HTTP status code

docker service logs docker-cluster-name_nginx | grep -w "HTTP/1.1\" 302"

Show last 5 records from the nginx service logs

docker service logs docker-cluster-name_nginx --tail 5
Last update September 15, 2023
Cheat Sheet docker
Do you have any questions?

Contact Me