diff --git a/docker/models/containers.py b/docker/models/containers.py index 493f1804..93f63725 100644 --- a/docker/models/containers.py +++ b/docker/models/containers.py @@ -18,6 +18,16 @@ class Container(Model): if self.attrs.get('Name') is not None: return self.attrs['Name'].lstrip('/') + @property + def image(self): + """ + The image of the container. + """ + image_id = self.attrs['Image'] + if image_id is None: + return None + return self.client.images.get(image_id.split(':')[1]) + @property def labels(self): """ diff --git a/docs/containers.rst b/docs/containers.rst index b67a066d..9ea64ad5 100644 --- a/docs/containers.rst +++ b/docs/containers.rst @@ -25,6 +25,7 @@ Container objects .. autoattribute:: short_id .. autoattribute:: name .. autoattribute:: status + .. autoattribute:: image .. autoattribute:: labels .. py:attribute:: attrs diff --git a/tests/unit/models_containers_test.py b/tests/unit/models_containers_test.py index a5ef4a11..c5946061 100644 --- a/tests/unit/models_containers_test.py +++ b/tests/unit/models_containers_test.py @@ -384,6 +384,11 @@ class ContainerTest(unittest.TestCase): container.get_archive('foo') client.api.get_archive.assert_called_with(FAKE_CONTAINER_ID, 'foo') + def test_image(self): + client = make_fake_client() + container = client.containers.get(FAKE_CONTAINER_ID) + assert container.image.id == FAKE_IMAGE_ID + def test_kill(self): client = make_fake_client() container = client.containers.get(FAKE_CONTAINER_ID)