How to list docker swarm nodes with labels

pbaranski picture pbaranski · Feb 23, 2017 · Viewed 14.7k times · Source

How can I easy print all available docker swarm nodes with their labels?

Added labels to nodes like
$ docker node update --label-add type=one my_node_name
And default listing nodes with docker node ls not showing filters.

Additionally I can list label inspecting each node like:

$ docker inspect my_node_name | grep type 
"type": "one"

---EDIT--

Similar question How do I filter docker swarm nodes by label? is about filtering my is about listing

Answer

BMitch picture BMitch · Feb 23, 2017

You can run something like:

docker node ls -q | xargs docker node inspect \
  -f '{{ .ID }} [{{ .Description.Hostname }}]: {{ .Spec.Labels }}'

You can adjust that to use a range for prettier formatting instead of printing the default map:

docker node ls -q | xargs docker node inspect \
  -f '{{ .ID }} [{{ .Description.Hostname }}]: {{ range $k, $v := .Spec.Labels }}{{ $k }}={{ $v }} {{end}}'

Feel free to update the formatted output to the fields you need.