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

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:

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.


These examples assume you have SSH access to nodes already setup — this is the default behaviour after a Docker Swarm cluster has been setup with Docker Community Edition or Docker Enterprise Edition — if you can’t access all the nodes via SSH when logged in to the cluster you should resolve that first.

Checking uptime and load on all nodes

Checking free memory (in MB) on all nodes

Get detailed info about all containers

Being able to view detailed info for all containers on the cluster can make identifying problematic services and resolving performance problems much easier.

You can easily get a list of all running containers using docker node:

However, if you want detailed information about CPU, memory, network and IO usage you can get that by combining docker node, cut, xargs and ssh then invoking docker stats on each node:

Hopefully this has been helpful. If you have any tips for good Docker management tools would be great if you could share them below!

Software for news and media and civic tech. Cat herder. Director at Glitch Digital.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store