How can I get the image ID (the docker sha256 hash) of a image / container within a Kubernetes deployment?
Something like this will do the trick (you must have jq
installed):
$ kubectl get pod --namespace=xx yyyy -o json | jq '.status.containerStatuses[] | { "image": .image, "imageID": .imageID }'
{
"image": "nginx:latest",
"imageID": "docker://sha256:b8efb18f159bd948486f18bd8940b56fd2298b438229f5bd2bcf4cedcf037448"
}
{
"image": "eu.gcr.io/zzzzzzz/php-fpm-5:latest",
"imageID": "docker://sha256:6ba3fe274b6110d7310f164eaaaaaaaaaa707a69df7324a1a0817fe3b475566a"
}
I love
jq
; as an additional trick (since the poster asked specifically about the sha) if your version ofjq
is new enough:{the_sha: .imageID|capture("docker://(?<s>sha256.+)").s}
, or you can usematch("docker://(sha256.+)").captures[0].string
in older versions; finally, in the specific example you used,jq
is ecma5-esque in that if your dest key and source key are named the same, the syntax{image, imageID}
will do what you thinkoh, and as much as I love
jq
, don't forget thatkubectl
has two different output templating languages, so if you only havekubectl
, it'll be hella verbose but you can use-o go-template='{{a ton of text here}}'
or-o jsonpath='more text with bizarre syntax'
Seems the JSON has changed with the newest version. The following seems to work instead now:
kubectl get pods -o json | jq '.items[] .status.containerStatuses[] | { "image": .image, "imageID": .imageID }'