30

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

P Ekambaram
  • 15,499
  • 7
  • 34
  • 59
pbaranski
  • 22,778
  • 19
  • 100
  • 117
  • Possible duplicate of [How do I filter docker swarm nodes by label?](http://stackoverflow.com/questions/40026638/how-do-i-filter-docker-swarm-nodes-by-label) – fzgregor Feb 23 '17 at 12:27

3 Answers3

73

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.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 1
    Awesome example! Just one question - is there any way to print hostname with a fixed length (padded with spaces), so all the labels are aligned in all the lines? Googled but didn't find. Something like `fmt.Printf("%-10v", value)` but in a go template – Ivan Mar 16 '20 at 16:43
  • 1
    @Ivan you can do something like `{{ printf "%25s" .Description.Hostname }}` – BMitch Mar 16 '20 at 18:30
  • @Ivan and BMitch (can only tag 1 of you in this comment): It is worth opening a separate question-with-answer for both your comments about go-templating the hostname output if such a Q&A doesn't yet exist. Others won't find that bit of info buried here in comments... – rowanthorpe Jan 23 '21 at 17:26
1

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

Allan K
  • 379
  • 2
  • 13
0

PowerShell Core version:

docker node ls -q | `
  ForEach-Object { `
    docker node inspect $_ --format '{{ json . }}' | `
    ConvertFrom-Json -Depth 99 } | `
  Select-Object { $_.Description.Hostname }, { $_.Spec.Labels }
ElMesa
  • 928
  • 8
  • 19