From 7beda716b3ad59ed3bd165b975afad245c84ced6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janek=20Nouvertn=C3=A9?= Date: Tue, 11 Feb 2025 09:59:07 +0100 Subject: [PATCH] ensure container metadata is available --- src/pytest_databases/_service.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/pytest_databases/_service.py b/src/pytest_databases/_service.py index 9e90578..c1e4dcf 100644 --- a/src/pytest_databases/_service.py +++ b/src/pytest_databases/_service.py @@ -164,7 +164,18 @@ def run( ulimits=ulimits, mem_limit=mem_limit, ) - container.reload() + + # reload the container; sometimes it can take a while before docker + # spins it up and the metadata becomes available, so we're redoing the + # check with a small incremental backup here + for i in range(10): + if any(v for v in container.ports.values()): + break + container.reload() + time.sleep(0.1 + (i / 10)) + else: + msg = f"Service {name!r} failed to create container" + raise ValueError(msg) host_port = int( container.ports[next(k for k in container.ports if k.startswith(str(container_port)))][0]["HostPort"]