Skip to content

Commit 24f9fed

Browse files
feat(container): move docker response dataclasses to docker_client.py
1 parent 69bd44b commit 24f9fed

File tree

7 files changed

+1158
-1
lines changed

7 files changed

+1158
-1
lines changed

core/testcontainers/compose/compose.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from types import TracebackType
1212
from typing import Any, Callable, Literal, Optional, TypeVar, Union, cast
1313

14+
from testcontainers.core.docker_client import ContainerInspectInfo, _ignore_properties
1415
from testcontainers.core.exceptions import ContainerIsNotRunning, NoSuchPortExposed
1516
from testcontainers.core.waiting_utils import WaitStrategy
1617

core/testcontainers/core/container.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from testcontainers.core.config import ConnectionMode
1616
from testcontainers.core.config import testcontainers_config as c
17-
from testcontainers.core.docker_client import DockerClient
17+
from testcontainers.core.docker_client import ContainerInspectInfo, DockerClient
1818
from testcontainers.core.exceptions import ContainerConnectException, ContainerStartException
1919
from testcontainers.core.labels import LABEL_SESSION_ID, SESSION_ID
2020
from testcontainers.core.network import Network
@@ -96,6 +96,7 @@ def __init__(
9696

9797
self._kwargs = kwargs
9898
self._wait_strategy: Optional[WaitStrategy] = _wait_strategy
99+
self._cached_container_info: Optional[ContainerInspectInfo] = None
99100

100101
def with_env(self, key: str, value: str) -> Self:
101102
self.env[key] = value
@@ -300,6 +301,24 @@ def exec(self, command: Union[str, list[str]]) -> ExecResult:
300301
raise ContainerStartException("Container should be started before executing a command")
301302
return self._container.exec_run(command)
302303

304+
def get_container_info(self) -> Optional[ContainerInspectInfo]:
305+
"""Get container information via docker inspect (lazy loaded)."""
306+
if self._cached_container_info is not None:
307+
return self._cached_container_info
308+
309+
if not self._container:
310+
return None
311+
312+
try:
313+
raw_data = self._container.attrs
314+
self._cached_container_info = ContainerInspectInfo.from_dict(raw_data)
315+
316+
except Exception as e:
317+
logger.warning(f"Failed to get container info for {self._container.id}: {e}")
318+
self._cached_container_info = None
319+
320+
return self._cached_container_info
321+
303322
def _configure(self) -> None:
304323
# placeholder if subclasses want to define this and use the default start method
305324
pass

0 commit comments

Comments
 (0)