docker / docker/docker-py

`container.image` fails when `ImageID` is not returned in the attributes for a container

Open
#3,144 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.2k
Forks
1.7k
Avg merge
13d 8h
Merged PRs (30d)
2

Description

See https://github.com/containers/podman/issues/18930 for how I found this.

The following code is erroneous:

    @property
    def image(self):
        """
        The image of the container.
        """
        image_id = self.attrs.get('ImageID', self.attrs['Image'])
        if image_id is None:
            return None
        return self.client.images.get(image_id.split(':')[1])

I believe it should be:

    @property
    def image(self):
        """
        The image of the container.
        """
        image_id = self.attrs.get('ImageID')
        if image_id is None:
            image = self.attrs.get('Image')
        else:
            image = image_id.split(':')[1]
        if image is None:
            return None
        return self.client.images.get(image)

Used against podman, the current code results in the following:

>>> container = client.containers.get('fa58eebd31bfe19add69043e3be368fed5a57d01965887d8d3b507a0ce42d89a')
>>> container.image.tags
Traceback (most recent call last):
  File "/home/circleci/.local/lib/python3.11/site-packages/docker/api/client.py", line 268, in _raise_for_status
    response.raise_for_status()
  File "/home/circleci/.local/lib/python3.11/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: http+docker://localhost/v1.40/images/latest/json

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/circleci/.local/lib/python3.11/site-packages/docker/models/containers.py", line 40, in image
    return self.client.images.get(image_id.split(':')[1])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.local/lib/python3.11/site-packages/docker/models/images.py", line 335, in get
    return self.prepare_model(self.client.api.inspect_image(name))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.local/lib/python3.11/site-packages/docker/utils/decorators.py", line 19, in wrapped
    return f(self, resource_id, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.local/lib/python3.11/site-packages/docker/api/image.py", line 251, in inspect_image
    return self._result(
           ^^^^^^^^^^^^^
  File "/home/circleci/.local/lib/python3.11/site-packages/docker/api/client.py", line 274, in _result
    self._raise_for_status(response)
  File "/home/circleci/.local/lib/python3.11/site-packages/docker/api/client.py", line 270, in _raise_for_status
    raise create_api_error_from_http_exception(e) from e
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/circleci/.local/lib/python3.11/site-packages/docker/errors.py", line 39, in create_api_error_from_http_exception
    raise cls(e, response=response, explanation=explanation) from e
docker.errors.ImageNotFound: 404 Client Error for http+docker://localhost/v1.40/images/latest/json: Not Found ("failed to find image latest: latest: No such image")

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in docker/models/containers.py at the Container.image property and compare the ImageID and Image attributes described in the issue. Reproduce the behavior against Podman if available, then verify that a missing ImageID falls back to Image without resolving the wrong image name.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.