Skip to content

Commit 1cd56b9

Browse files
committed
Adds a 'labels' property to the container model
The Docker API seems to respond with a 'null' value for the 'Labels' attribute from containers that were created with older Docker versions. An empty dictionary is returned in this case. Signed-off-by: Frank Sachsenheim <[email protected]>
1 parent 6529fa5 commit 1cd56b9

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

docker/models/containers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ def name(self):
1818
if self.attrs.get('Name') is not None:
1919
return self.attrs['Name'].lstrip('/')
2020

21+
@property
22+
def labels(self):
23+
"""
24+
The labels of a container as dictionary.
25+
"""
26+
result = self.attrs['Config'].get('Labels')
27+
return result or {}
28+
2129
@property
2230
def status(self):
2331
"""

docs/containers.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Container objects
2525
.. autoattribute:: short_id
2626
.. autoattribute:: name
2727
.. autoattribute:: status
28+
.. autoattribute:: labels
2829
.. py:attribute:: attrs
2930
3031
The raw representation of this object from the server.

tests/unit/fake_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def get_fake_inspect_container(tty=False):
134134
status_code = 200
135135
response = {
136136
'Id': FAKE_CONTAINER_ID,
137-
'Config': {'Privileged': True, 'Tty': tty},
137+
'Config': {'Labels': {'foo': 'bar'}, 'Privileged': True, 'Tty': tty},
138138
'ID': FAKE_CONTAINER_ID,
139139
'Image': 'busybox:latest',
140140
'Name': 'foobar',

tests/unit/models_containers_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ def test_kill(self):
390390
container.kill(signal=5)
391391
client.api.kill.assert_called_with(FAKE_CONTAINER_ID, signal=5)
392392

393+
def test_labels(self):
394+
client = make_fake_client()
395+
container = client.containers.get(FAKE_CONTAINER_ID)
396+
assert container.labels == {'foo': 'bar'}
397+
393398
def test_logs(self):
394399
client = make_fake_client()
395400
container = client.containers.get(FAKE_CONTAINER_ID)

0 commit comments

Comments
 (0)