docker service inspect --format [would like to iterate Array using Go templates]

Viewed 304

I am trying to get specific information from 'docker service inspect' command and I like json format but I am noob in go templates and I do not know how to iterate across an array and get the object properties, I would appreciate any help on this. ah! I cannot install anything, IT policies :( so need to work with what I have.

I have this so far, as you can see I am able to access the n element in the array (Secrets) but I don't know how to fully iterate Secrets

docker service inspect --format='{{(index .Spec.TaskTemplate.ContainerSpec.Secrets 0).SecretName}}' <paste_your_service_id>

The final goal is knowing what service uses what secret (the first service matching would work) then finding a way to connect to the first worker node running the container and run "docker exec -t <container_id> cat <secret_path>" for the secretName I am looking for.

Thanks in advance!

1 Answers

If you want to do it for a single service, you could use range to iterate over the secrets.

fmt='{{ .Spec.Name }} {{ range .Spec.TaskTemplate.ContainerSpec.Secrets }}{{ .SecretName }} {{ end }}'
docker service inspect <some-service> --format "$fmt"

The format won't work on service ls, though. Because docker is not exposing the full object.

The workaround could be xargs. This will potentially make a lot of calls to the docker engine. Because it will do an inspect for each service in the list. So be careful.

docker service ls -q | xargs -I{} docker service inspect {} --format "$fmt"

For debugging, the json function is really useful. It helps you to figure out what is available with its keys.

docker service ls --format '{{ json . }}'
Related