Reputation: 12999
I'm looking for a single-line command to find all of NodeID
, ContainerID
and Hostname
for all containers in a docker swarm that relate to service srv-myservice
.
I've got as far as collecting the first two bits of info (NodeID
, ContainerID
) using docker service ls
:
# SRV=srv-myservice; for f in $(docker service ps -q ${SRV} -f desired-state=running); do docker inspect --format 'NodeID: {{.NodeID}}, ContainerID: {{.Status.ContainerStatus.ContainerID}}' $f; done
NodeID: p49jhk0wliix4857u9pajbqlr, ContainerID: 41c1b5d4cfd899d96f0ca78e798636a6d4c7ea11002b69eee7bb0a78858c1c7e
And the last bit of info (Hostname
) from docker node ls
, along with NodeID
to provide a link to the above:
# docker node ls --format 'NodeID: {{.ID}}, Hostname: {{.Hostname}}'
NodeID: p49jhk0wliix4857u9pajbqlr, Hostname: myserver-01
NodeID: 1cd89df9c89cb8477b3050ced, Hostname: myserver-02
I can then manually get the Hostname
from the matching NodeID
and put it together with the other info to get what I want:
Hostname: myserver-01, NodeID: p49jhk0wliix4857u9pajbqlr, ContainerID: 41c1b5d4cfd899d96f0ca78e798636a6d4c7ea11002b69eee7bb0a78858c1c7e
... but my abilities to merge these two steps into a single command-line statement are slightly lacking.
Upvotes: 0
Views: 439
Reputation: 12877
You can use Awk for this:
awk 'NR==FNR { map[$2]=$4;next } { print "Hostname: "map[$2]", "$0 } ' <(docker node ls --format 'NodeID: {{.ID}}, Hostname: {{.Hostname}}') <(for f in $(docker service ps -q ${SRV} -f desired-state=running); do docker inspect --format 'NodeID: {{.NodeID}}, ContainerID: {{.Status.ContainerStatus.ContainerID}}' $f; done)
Explantion:
awk 'NR==FNR {
map[$2]=$4; # Process the first output (NR==FNR) - Set up an array map with the node id as the index and the host name as the value
next ; # Skip to the next record
}
{
print "Hostname: "map[$2]", "$0 # Process the second output, print the hostname from the map array along with the rest of the line
} ' <(docker node ls --format 'NodeID: {{.ID}}, Hostname: {{.Hostname}}') <(for f in $(docker service ps -q ${SRV} -f desired-state=running); do docker inspect --format 'NodeID: {{.NodeID}}, ContainerID: {{.Status.ContainerStatus.ContainerID}}' $f; done)
Upvotes: 1