Skip to content

Commit 71cff86

Browse files
fix: get_wrapped_container returns container all the time now
1 parent 5c1504c commit 71cff86

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

core/testcontainers/compose/compose.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from types import TracebackType
1212
from typing import Any, Callable, Literal, Optional, TypeVar, Union, cast
1313

14+
from docker import DockerClient
15+
from docker.models.containers import Container
1416
from testcontainers.core.exceptions import ContainerIsNotRunning, NoSuchPortExposed
1517
from testcontainers.core.waiting_utils import WaitStrategy
1618

@@ -137,9 +139,9 @@ def get_logs(self) -> tuple[bytes, bytes]:
137139
stdout, stderr = self._docker_compose.get_logs(self.Service)
138140
return stdout.encode(), stderr.encode()
139141

140-
def get_wrapped_container(self) -> "ComposeContainer":
142+
def get_wrapped_container(self) -> Container:
141143
"""Get the underlying container object for compatibility."""
142-
return self
144+
return self._docker_compose._get_docker_client().containers.get(self.ID)
143145

144146
def reload(self) -> None:
145147
"""Reload container information for compatibility with wait strategies."""
@@ -214,7 +216,9 @@ class DockerCompose:
214216
services: Optional[list[str]] = None
215217
docker_command_path: Optional[str] = None
216218
profiles: Optional[list[str]] = None
219+
docker_client_kw: Optional[dict[str, Any]] = None
217220
_wait_strategies: Optional[dict[str, Any]] = field(default=None, init=False, repr=False)
221+
_docker_client: Optional[DockerClient] = field(default=None, init=False, repr=False)
218222

219223
def __post_init__(self) -> None:
220224
if isinstance(self.compose_file_name, str):
@@ -259,6 +263,13 @@ def compose_command_property(self) -> list[str]:
259263
docker_compose_cmd += ["--env-file", env_file]
260264
return docker_compose_cmd
261265

266+
def _get_docker_client(self) -> DockerClient:
267+
dc = self._docker_client
268+
if dc is None:
269+
dc = DockerClient(**(self.docker_client_kw or {}))
270+
self._docker_client = dc
271+
return dc
272+
262273
def waiting_for(self, strategies: dict[str, WaitStrategy]) -> "DockerCompose":
263274
"""
264275
Set wait strategies for specific services.

core/testcontainers/core/waiting_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from typing import Any, Callable, Optional, Protocol, TypeVar, Union, cast
2121

2222
import wrapt
23+
from docker.models.containers import Container
2324
from typing_extensions import Self
2425

2526
from testcontainers.core.config import testcontainers_config
@@ -52,7 +53,7 @@ def get_exposed_port(self, port: int) -> int:
5253
"""Get the exposed port mapping for the given internal port."""
5354
...
5455

55-
def get_wrapped_container(self) -> Any:
56+
def get_wrapped_container(self) -> Container:
5657
"""Get the underlying container object."""
5758
...
5859

0 commit comments

Comments
 (0)