Fix Container.image resolving the wrong image - #3426
Open
UditDewan wants to merge 1 commit into
Open
Conversation
`Container.image` assumed the value it looked up was always a digest and
split it on the first colon. That is only true for `ImageID`. Daemons that
omit `ImageID` report an image name in `Image` instead, so `busybox:latest`
was split into `latest` and looked up as if it were an image ID, raising
`ImageNotFound` (or silently returning an unrelated image that happens to be
tagged `latest`).
Strip only the `sha256:` digest prefix and pass anything else through
untouched, matching how `Image.short_id` already treats these values.
Also read `Image` with `.get()`. It was passed as the default argument to
`self.attrs.get('ImageID', ...)`, which Python evaluates eagerly, so a
container whose attributes had `ImageID` but no `Image` raised `KeyError`
instead of using the `ImageID` that was present.
Fixes docker#3144
Signed-off-by: uditDewan <udit.dewan21@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3144
What
Container.imagesplits the value it looks up on the first colon and keeps the second half:That is correct only when the value is a digest (
sha256:<hex>). Daemons that don't reportImageIDput an image name inImageinstead, sobusybox:latestgets split intolatest, which is then looked up as an image ID:The failure isn't always loud — if some unrelated image happens to be tagged
latest, the wrongImageis returned silently.There is a second, smaller bug on the same line:
self.attrs['Image']is the default argument to.get(), and Python evaluates it eagerly. A container whose attributes carryImageIDbut noImageraisesKeyError: 'Image'even though theImageIDneeded to answer the call is right there.How
Strip only the
sha256:prefix and pass anything else through untouched. This matches howImage.short_idandImage.tagalready discriminate these values, so no new parsing helper is introduced.The requests sent for inputs that already worked are byte-for-byte unchanged — a
sha256:-prefixedImageIDorImagestill resolves to the same bare hex ID as before. Only the previously-broken name case changes behaviour. TheImagelookup also moves to.get()so the eager-defaultKeyErrorgoes away.Testing
Three unit tests in
tests/unit/models_containers_test.pycovering name, digest, and neither-key-present. Against the current code they fail with exactly the reported symptoms:Worth noting that the existing
test_imagepassed only by accident: the fake API returns the same image for any name, so the boguslatestlookup went unnoticed. The added assertion pins the argument rather than just the result.Full unit suite passes. Three unrelated failures (
test_log_since_with_datetime,test_events_with_since_until,test_kwargs_from_env_no_cert_path) reproduce on a clean checkout ofmainon Windows/non-UTC and are tracked in #3335.