Skip to content

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

Description

@cjw296

See podman-container-tools/podman#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")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions