buildx icon indicating copy to clipboard operation
buildx copied to clipboard

imagetools inspect `--format` ignores formatting unless `json` is used

Open thaJeztah opened this issue 2 years ago • 3 comments

Was writing a reply to https://github.com/docker/hub-feedback/issues/2043, and noticed this.

Reproduced on;

docker buildx version
github.com/docker/buildx v0.8.2-docker 6224def4dd2c3d347eee19db595348c50d7cb491

When using the -f / --format option, the template is ignored, unless it's using the json function;

docker buildx imagetools inspect --format '{{.Manifest.Digest}}' tensorflow/tensorflow:1.13.1-gpu-py3
Name:      docker.io/tensorflow/tensorflow:1.13.1-gpu-py3
MediaType: application/vnd.docker.distribution.manifest.v2+json
Digest:    sha256:0f949ccc690d9c50e9b46b16d9030c2c6845af621c2e7e82c4bf59803edc69b5
docker buildx imagetools inspect --format '{{json .Manifest.Digest}}' tensorflow/tensorflow:1.13.1-gpu-py3
"sha256:0f949ccc690d9c50e9b46b16d9030c2c6845af621c2e7e82c4bf59803edc69b5"

This is inconsistent with other commands, for example, here's docker image inspect (difference between using json and without is that the json output uses JSON quoting for the output);

docker image inspect -f '{{.ID}}' busybox
sha256:62aedd01bd8520c43d06b09f7a0f67ba9720bdc04631a8242c65ea995f3ecac8

docker image inspect -f '{{json .ID}}' busybox
"sha256:62aedd01bd8520c43d06b09f7a0f67ba9720bdc04631a8242c65ea995f3ecac8"

Also note that the json output does not print a newline at the end, which can make it a bit cumbersome to use; here's the full output (notice that the prompt is appended to the output, because there's no trailing newline)

root@swarm-test-01:~# docker buildx imagetools inspect --format '{{json .Manifest.Digest}}' tensorflow/tensorflow:1.13.1-gpu-py3
"sha256:0f949ccc690d9c50e9b46b16d9030c2c6845af621c2e7e82c4bf59803edc69b5"root@swarm-test-01:~#

thaJeztah avatar Jun 21 '22 10:06 thaJeztah

/cc @crazy-max @jedevc

thaJeztah avatar Jun 21 '22 10:06 thaJeztah

@thaJeztah Since https://github.com/docker/cli/pull/2936 has been merged I will revisit the format output to be aligned.

crazy-max avatar Jun 21 '22 11:06 crazy-max

Thanks! Yeah, I was mostly surprised it was silently ignored 😅. w.r.t. the newline at the end; technically it's "correct" (the newline doesn't have to be there (also not when using it for scripting), but ISTR the newline was added to make it slightly easier to use interactively.

thaJeztah avatar Jun 21 '22 11:06 thaJeztah

Hey! It looks like this issue is coming up on a year old, is this fixable now given the aforementioned PR was merged? I still see this behaviour today.

❯ docker buildx imagetools inspect debian:bullseye --format="{{.Manifest.Digest}}"
Name:      docker.io/library/debian:bullseye
MediaType: application/vnd.docker.distribution.manifest.list.v2+json
Digest:    sha256:1e5f2d70c9441c971607727f56d0776fb9eecf23cd37b595b26db7a974b2301d
           
Manifests:
...

uhthomas avatar Jun 13 '23 22:06 uhthomas

The linked PR was implementing changes in the docker CLI itself; this probably hasn't been worked on for buildx. Contributions welcome though if someone is interested working on this.

thaJeztah avatar Jun 13 '23 22:06 thaJeztah

Sure, I'm taking a look now. There's definitely something funky going on given it doesn't seem that Go templates for {{.Manifest are respected and includes duplicate logic.

uhthomas avatar Jun 14 '23 14:06 uhthomas

Cool, I removed that logic and it seems to work as expected. I'll clean it up and submit a PR.

❯ go run ./cmd/buildx imagetools inspect --format="{{.Manifest.Digest}}" alpine
sha256:02bb6f428431fbc2809c5d1b41eab5a68350194fb508869a33cb1af4444c9b11

uhthomas avatar Jun 14 '23 14:06 uhthomas