How to run a command on every node in a Docker Swarm cluster

Iain Collins
3 min readNov 15, 2017

Docker Swarm is a great way to deploy Docker Containers on AWS or Azure, both of which are supported cloud providers for Docker Community Edition (CE) and Enterprise Edition (EE).

A common admin task can be needing to run a command on all nodes on a cluster, either for monitoring or when debugging problems.

Executing Containers on all nodes in a cluster is easy, and the docker stats command lets you easily view the memory, CPU, network and IO usage of all Containers on an individual node.

However, unless you are running the Enterprise Edition or something like the Portainer management UI, there is easy way to see the status of all containers at once — and there is no built-in docker command for running commands directly on nodes.

Combine docker, cut and xargs

If you have a Docker Swarm cluster you will already be familiar with using the docker node command to list nodes in the cluster:

> docker node ls
ID HOSTNAME STATUS
2v77j8x4ljejeeku9tao00tcw * swarm-manager000000 Ready ox633ovtcp5ux986fb30x6d6x swarm-manager000001 Ready
qfig0i98hhxdekf7wfld8jo7q swarm-worker000000 Ready

If you want to run commands on all nodes in a cluster, it’s pretty easy to combine this output with xargs using cut and ssh.

Examples

--

--