diff --git a/core/testcontainers/core/container.py b/core/testcontainers/core/container.py index 6bf2f5b4..f677182f 100644 --- a/core/testcontainers/core/container.py +++ b/core/testcontainers/core/container.py @@ -12,7 +12,7 @@ from testcontainers.core.config import ConnectionMode from testcontainers.core.config import testcontainers_config as c from testcontainers.core.docker_client import DockerClient -from testcontainers.core.exceptions import ContainerStartException +from testcontainers.core.exceptions import ContainerConnectException, ContainerStartException from testcontainers.core.labels import LABEL_SESSION_ID, SESSION_ID from testcontainers.core.network import Network from testcontainers.core.utils import is_arm, setup_logger @@ -228,15 +228,21 @@ def _create_instance(cls) -> "Reaper": .with_env("RYUK_RECONNECTION_TIMEOUT", c.ryuk_reconnection_timeout) .start() ) - wait_for_logs(Reaper._container, r".* Started!") + wait_for_logs(Reaper._container, r".* Started!", timeout=20, raise_on_exit=True) container_host = Reaper._container.get_container_host_ip() container_port = int(Reaper._container.get_exposed_port(8080)) + if not container_host or not container_port: + raise ContainerConnectException( + f"Could not obtain network details for {Reaper._container._container.id}. Host: {container_host} Port: {container_port}" + ) + last_connection_exception: Optional[Exception] = None for _ in range(50): try: Reaper._socket = socket() + Reaper._socket.settimeout(1) Reaper._socket.connect((container_host, container_port)) last_connection_exception = None break diff --git a/core/testcontainers/core/exceptions.py b/core/testcontainers/core/exceptions.py index 6694e598..99ea9881 100644 --- a/core/testcontainers/core/exceptions.py +++ b/core/testcontainers/core/exceptions.py @@ -16,6 +16,10 @@ class ContainerStartException(RuntimeError): pass +class ContainerConnectException(RuntimeError): + pass + + class ContainerIsNotRunning(RuntimeError): pass