|
14 | 14 |
|
15 | 15 | from testcontainers.core.config import ConnectionMode |
16 | 16 | 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 |
18 | 18 | from testcontainers.core.exceptions import ContainerConnectException, ContainerStartException |
19 | 19 | from testcontainers.core.labels import LABEL_SESSION_ID, SESSION_ID |
20 | 20 | from testcontainers.core.network import Network |
@@ -96,6 +96,7 @@ def __init__( |
96 | 96 |
|
97 | 97 | self._kwargs = kwargs |
98 | 98 | self._wait_strategy: Optional[WaitStrategy] = _wait_strategy |
| 99 | + self._cached_container_info: Optional[ContainerInspectInfo] = None |
99 | 100 |
|
100 | 101 | def with_env(self, key: str, value: str) -> Self: |
101 | 102 | self.env[key] = value |
@@ -300,6 +301,24 @@ def exec(self, command: Union[str, list[str]]) -> ExecResult: |
300 | 301 | raise ContainerStartException("Container should be started before executing a command") |
301 | 302 | return self._container.exec_run(command) |
302 | 303 |
|
| 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 | + |
303 | 322 | def _configure(self) -> None: |
304 | 323 | # placeholder if subclasses want to define this and use the default start method |
305 | 324 | pass |
|
0 commit comments