Skip to content

Commit be2620c

Browse files
committed
feat: add ContainerInfo--object as return object to inspect_container
1 parent db7f8b8 commit be2620c

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

docker/api/container.py

+36-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,37 @@
1111
)
1212

1313

14+
class ContainerInfo:
15+
def __init__(self, info: dict):
16+
self._info = info
17+
18+
def __repr__(self):
19+
return (
20+
f"<ContainerInfo id={self._info.get('Id')}, name={self._info.get('Name')}>"
21+
)
22+
23+
def __getattr__(self, item):
24+
"""
25+
Dynamically fetch any attribute from the underlying dictionary.
26+
This allows direct access to all fields without manually defining them.
27+
"""
28+
try:
29+
return self._info[item]
30+
except KeyError as err:
31+
raise AttributeError(
32+
f"'ContainerInfo' object has no attribute '{item}'"
33+
) from err
34+
35+
def __getitem__(self, item):
36+
"""
37+
Optional: If you'd like to access attributes using bracket notation.
38+
"""
39+
return self._info.get(item)
40+
41+
def get_info(self):
42+
"""Returns the entire dictionary for reference if needed"""
43+
return self._info
44+
1445
class ContainerApiMixin:
1546
@utils.check_resource('container')
1647
def attach(self, container, stdout=True, stderr=True,
@@ -783,16 +814,18 @@ def inspect_container(self, container):
783814
container (str): The container to inspect
784815
785816
Returns:
786-
(dict): Similar to the output of `docker inspect`, but as a
787-
single dict
817+
(ContainerInfo): Similar to the output of `docker inspect`, but as a
818+
docker.api.container.ContainerInfo object
788819
789820
Raises:
790821
:py:class:`docker.errors.APIError`
791822
If the server returns an error.
792823
"""
793-
return self._result(
824+
container_info = self._result(
794825
self._get(self._url("/containers/{0}/json", container)), True
795826
)
827+
return ContainerInfo(container_info)
828+
796829

797830
@utils.check_resource('container')
798831
def kill(self, container, signal=None):

tests/integration/api_container_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -1572,3 +1572,19 @@ def test_remove_link(self):
15721572
x['Id'].startswith(container2_id)
15731573
]
15741574
assert len(retrieved) == 2
1575+
1576+
class ContainerInfoObjectTest(BaseAPIIntegrationTest):
1577+
def test_container_info_object(self):
1578+
container = self.client.create_container(
1579+
TEST_IMG, 'true', host_config=self.client.create_host_config())
1580+
self.tmp_containers.append(container)
1581+
self.client.start(container)
1582+
1583+
inspect_data = self.client.inspect_container(container)
1584+
assert isinstance(inspect_data, docker.api.container.ContainerInfo)
1585+
assert inspect_data['Config']['Image'] == TEST_IMG
1586+
assert inspect_data['HostConfig']['NetworkMode'] == 'bridge'
1587+
1588+
# attribute style access
1589+
assert inspect_data.Id == container['Id']
1590+
assert inspect_data["Id"] == container["Id"]

0 commit comments

Comments
 (0)