diff --git a/docker/models/containers.py b/docker/models/containers.py index 0c2b855a..7ae1ad34 100644 --- a/docker/models/containers.py +++ b/docker/models/containers.py @@ -28,6 +28,8 @@ class Container(Model): """ if self.attrs.get('Name') is not None: return self.attrs['Name'].lstrip('/') + if self.attrs.get('Names') is not None: + return self.attrs['Names'][0].lstrip('/') @property def image(self): diff --git a/tests/integration/models_containers_test.py b/tests/integration/models_containers_test.py index eac4c979..c9e0d06b 100644 --- a/tests/integration/models_containers_test.py +++ b/tests/integration/models_containers_test.py @@ -210,7 +210,7 @@ class ContainerCollectionTest(BaseIntegrationTest): def test_list_sparse(self): client = docker.from_env(version=TEST_API_VERSION) container_id = client.containers.run( - "alpine", "sleep 300", detach=True).id + "alpine", "sleep 300", detach=True, name='a_container').id self.tmp_containers.append(container_id) containers = [c for c in client.containers.list(sparse=True) if c.id == container_id] @@ -220,6 +220,7 @@ class ContainerCollectionTest(BaseIntegrationTest): assert container.attrs['Image'] == 'alpine' assert container.status == 'running' assert container.image == client.images.get('alpine') + assert container.name == 'a_container' with pytest.raises(docker.errors.DockerException): container.labels