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
Run docker node update --label-add on a manager node to add label metadata to a node. The --label-add flag supports either a <key> or a <key>=<value> pair. The labels you set for nodes using docker node update apply only to the node entity within the swarm. Do not confuse them with the docker daemon labels for dockerd.
To check the labels of a particular Image, you can use the Docker Inspect command. Start the Docker Container. Execute the Inspect Command. Inside the LABELS object, you can find all the labels associated with the image that you have specified inside your Dockerfile.
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.
My answer here is not what the OP had asked for (that has been answered by @BMitch). Just to add on @BMitch answer I extended the code to provide some additional node information I found useful hoping it may be useful to someone else.
docker node ls -q | xargs docker node inspect -f '{{ .ID }} [hostname={{ .Description.Hostname }}, Addr={{ .Status.Addr }}, State={{ .Status.State }}, Role={{ .Spec.Role }}, Availability={{ .Spec.Availability }}]: Arch={{ .Description.Platform.Architecture }}, OS={{ .Description.Platform.OS }}, NanoCPUs={{ .Description.Resources.NanoCPUs }}, MemoryBytes={{ .Description.Resources.MemoryBytes }}, docker_version={{ .Description.Engine.EngineVersion }}, labels={{ range $k, $v := .Spec.Labels }}{{ $k }}={{ $v }} {{end}}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With