From 31f3f683592d1ffb70b7ab67231afae0c4604187 Mon Sep 17 00:00:00 2001 From: Svet Date: Tue, 11 Feb 2025 14:15:59 +0200 Subject: [PATCH] Fix for multiple container start invocations with custom labels When invoking .start() multiple times on the same DockerContainer instance, the call fails with "ValueError: The org.testcontainers namespace is reserved for internal use" error. Example code: ``` from testcontainers.core.container import DockerContainer container = DockerContainer("alpine:latest").with_kwargs(labels={}) container.start() container.stop() container.start() ``` The fix is to update labels for the container in a copy of the user-provided dictionary, so that: * the code doesn't mutate user structures * avoid side effects, allowing for multiple .start() invocations --- core/testcontainers/core/labels.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/core/testcontainers/core/labels.py b/core/testcontainers/core/labels.py index 0570b22cb..1c45b79cf 100644 --- a/core/testcontainers/core/labels.py +++ b/core/testcontainers/core/labels.py @@ -21,12 +21,15 @@ def create_labels(image: str, labels: Optional[dict[str, str]]) -> dict[str, str if k.startswith(TESTCONTAINERS_NAMESPACE): raise ValueError("The org.testcontainers namespace is reserved for internal use") - labels[LABEL_LANG] = "python" - labels[LABEL_TESTCONTAINERS] = "true" - labels[LABEL_VERSION] = importlib.metadata.version("testcontainers") + tc_labels = { + **labels, + LABEL_LANG: "python", + LABEL_TESTCONTAINERS: "true", + LABEL_VERSION: importlib.metadata.version("testcontainers"), + } if image == c.ryuk_image: - return labels + return tc_labels - labels[LABEL_SESSION_ID] = SESSION_ID - return labels + tc_labels[LABEL_SESSION_ID] = SESSION_ID + return tc_labels