Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list docker swarm nodes with labels

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

like image 295
pbaranski Avatar asked Feb 23 '17 11:02

pbaranski


People also ask

How do I add a label to swarm node?

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.

How do I view docker image labels?

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.


2 Answers

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.

like image 60
BMitch Avatar answered Oct 23 '22 11:10

BMitch


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}}'

like image 1
Allan K Avatar answered Oct 23 '22 10:10

Allan K