diff --git a/docker/models/images.py b/docker/models/images.py index 51ee6f4a..3fd3dc19 100644 --- a/docker/models/images.py +++ b/docker/models/images.py @@ -15,6 +15,14 @@ class Image(Model): def __repr__(self): return "<%s: '%s'>" % (self.__class__.__name__, "', '".join(self.tags)) + @property + def labels(self): + """ + The labels of an image as dictionary. + """ + result = self.attrs['Config'].get('Labels') + return result or {} + @property def short_id(self): """ diff --git a/docs/images.rst b/docs/images.rst index 25fcffc8..44f006c0 100644 --- a/docs/images.rst +++ b/docs/images.rst @@ -29,6 +29,7 @@ Image objects .. autoattribute:: id .. autoattribute:: short_id .. autoattribute:: tags + .. autoattribute:: labels .. py:attribute:: attrs The raw representation of this object from the server. diff --git a/tests/unit/fake_api.py b/tests/unit/fake_api.py index 2914b63a..ff0f1b65 100644 --- a/tests/unit/fake_api.py +++ b/tests/unit/fake_api.py @@ -158,6 +158,7 @@ def get_fake_inspect_image(): 'Parent': "27cf784147099545", 'Created': "2013-03-23T22:24:18.818426-07:00", 'Container': FAKE_CONTAINER_ID, + 'Config': {'Labels': {'bar': 'foo'}}, 'ContainerConfig': { "Hostname": "", diff --git a/tests/unit/models_images_test.py b/tests/unit/models_images_test.py index efb21166..784717be 100644 --- a/tests/unit/models_images_test.py +++ b/tests/unit/models_images_test.py @@ -21,6 +21,11 @@ class ImageCollectionTest(unittest.TestCase): assert isinstance(image, Image) assert image.id == FAKE_IMAGE_ID + def test_labels(self): + client = make_fake_client() + image = client.images.get(FAKE_IMAGE_ID) + assert image.labels == {'bar': 'foo'} + def test_list(self): client = make_fake_client() images = client.images.list(all=True)