Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return image ID from docker image load command #2212

Open
codeseedr opened this issue Dec 4, 2019 · 7 comments
Open

Return image ID from docker image load command #2212

codeseedr opened this issue Dec 4, 2019 · 7 comments

Comments

@codeseedr
Copy link

Description

I propose that docker image load return, apart from the image name, the assigned image ID. That would save an extra step of listing images in order to find it. It would also make the command consistent with container creation which does return container ID.

Describe the results you received:

$ docker image load --input <file-path>
Loaded image: <image-name>

Describe the results you expected:
I'd prefer to see also the image ID being returned.

@thaJeztah
Copy link
Member

This requires a change in the API code to return the information.

Looks like the API currently returns either;

  • a response for each image name / tag (Loaded image: foo:bar)
  • if no tags were found, the image-ID (Loaded image ID: <image id>)

https://github.com/moby/moby/blob/ad1b781e44fa1e44b9e654e5078929aec56aed66/image/tarexport/load.go#L129

imageIDsStr += fmt.Sprintf("Loaded image ID: %s\n", imgID)

https://github.com/moby/moby/blob/ad1b781e44fa1e44b9e654e5078929aec56aed66/image/tarexport/load.go#L158-L160

if imageRefCount == 0 {
	outStream.Write([]byte(imageIDsStr))
}

Looks like technically it's not difficult to return both, but having some concerns about that;

  • could be confusing (printing a single line in one case, and multiple lines in the other case; if could also be confusing because sometimes a single image-ID can have multiple tags)
  • printing both "name" and "ID" on a single line would work around part of that confusion (Loaded image: <image-name> (ID: <image-id>)
  • Although the output is not strictly "formalised", both of the above (if done by default) likely can cause a breaking change for those that consume the output in scripts
    • adding a --format option could work around that
  • docker image load doesn't currently preserve registry-digests (image@sha256:deadbeef..), which would've been an alternative and a non-breaking change (we could print foo/image:tag@sha256:deadbeef, which would provide both a mutable (image:tag) and an immutable (sha256:deadbeef..) reference. This might be supported in future once integration with the containerd store is completed.

I see there's a tests that validate that exact behavior (https://github.com/moby/moby/blob/ad1b781e44fa1e44b9e654e5078929aec56aed66/integration-cli/docker_cli_save_load_test.go#L395-L405)

Looking at the history of that one, I see it was added in Docker 1.12 (through moby/moby#23377, which was a carry of moby/moby#23007), and has been around for a while, which increases the risk of a change being potentially breaking.

@codeseedr
Copy link
Author

Thanks for a prompt and thorough analysis.

I think all the info (in the single tag printing mode at least) should be provided on a single line. However, I admit the change in format is a concern, so to avoid breakage the new, more informative format should be introduced with a cmd line switch: -v/--verbose or -f/--format like you suggested, with the latter taking a parameter it would also enable us to request multiple tags be printed (as separate line entries?) or not.

@codeseedr
Copy link
Author

Any chance to have any solution implemented within any reasonable period?

@codeseedr
Copy link
Author

I would definitely appreciate an update.

@codeseedr
Copy link
Author

Bump.

@virusdave
Copy link

Any progress? This would be highly useful for us.

@jbuhacoff
Copy link

jbuhacoff commented Oct 27, 2021

Using Docker version 20.10.9, it already prints "Loaded image: " on stdout, so you can pipe it to other shell commands to get the ID instead:

sudo docker load -i $file \
| sed -nr 's/^Loaded image: (.*)$/\1/p' \
| xargs -I{} sudo docker image ls "{}" --format="{{.ID}}"
  1. Output of docker load ends with "Loaded image: $name"
  2. Pipe to sed to extract the $name from that
  3. Pipe to xargs to convert that to an argument for docker image ls --format="{{.ID}}" which prints only the image ID for the given image name

Ocramius added a commit to Ocramius/oci-srm-server-mock-rust that referenced this issue Sep 4, 2023
Also, loading + extracting image ID in one line.

Ref: docker/cli#2212 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants