Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions core/testcontainers/compose/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from types import TracebackType
from typing import Any, Callable, Literal, Optional, TypeVar, Union, cast

from docker import DockerClient
from docker.models.containers import Container
from testcontainers.core.exceptions import ContainerIsNotRunning, NoSuchPortExposed
from testcontainers.core.waiting_utils import WaitStrategy

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

def get_wrapped_container(self) -> "ComposeContainer":
def get_wrapped_container(self) -> Container:
"""Get the underlying container object for compatibility."""
return self
return self._docker_compose._get_docker_client().containers.get(self.ID)
Copy link

@surister surister Oct 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dumb question: can .containers.get(self.ID) return a value other than Container, e.g. None?

what if the container is not found?


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

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

def _get_docker_client(self) -> DockerClient:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def _get_docker_client(self) -> DockerClient:
def _get_docker_client(self) -> DockerClient:
if self._docker_client is None:
self._docker_client = DockerClient(**(self.docker_client_kw or {}))
return self._docker_client

dc = self._docker_client
if dc is None:
dc = DockerClient(**(self.docker_client_kw or {}))
self._docker_client = dc
return dc

def waiting_for(self, strategies: dict[str, WaitStrategy]) -> "DockerCompose":
"""
Set wait strategies for specific services.
Expand Down
3 changes: 2 additions & 1 deletion core/testcontainers/core/waiting_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from typing import Any, Callable, Optional, Protocol, TypeVar, Union, cast

import wrapt
from docker.models.containers import Container
from typing_extensions import Self

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

def get_wrapped_container(self) -> Any:
def get_wrapped_container(self) -> Container:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We recently had a PR that added a bunch of data classes, I think I should actually be returning one of those here

"""Get the underlying container object."""
...

Expand Down
Loading