diff --git a/core/testcontainers/core/container.py b/core/testcontainers/core/container.py index 90967e95..6bf2f5b4 100644 --- a/core/testcontainers/core/container.py +++ b/core/testcontainers/core/container.py @@ -1,10 +1,12 @@ import contextlib +from os import PathLike from socket import socket from typing import TYPE_CHECKING, Optional, Union import docker.errors from docker import version from docker.types import EndpointConfig +from dotenv import dotenv_values from typing_extensions import Self, assert_never from testcontainers.core.config import ConnectionMode @@ -57,6 +59,12 @@ def with_env(self, key: str, value: str) -> Self: self.env[key] = value return self + def with_env_file(self, env_file: Union[str, PathLike]) -> Self: + env_values = dotenv_values(env_file) + for key, value in env_values.items(): + self.with_env(key, value) + return self + def with_bind_ports(self, container: int, host: Optional[int] = None) -> Self: self.ports[container] = host return self diff --git a/core/tests/test_core.py b/core/tests/test_core.py index 42321e28..9312b0bc 100644 --- a/core/tests/test_core.py +++ b/core/tests/test_core.py @@ -1,3 +1,6 @@ +import tempfile +from pathlib import Path + from testcontainers.core.container import DockerContainer @@ -17,3 +20,29 @@ def test_get_logs(): assert isinstance(stdout, bytes) assert isinstance(stderr, bytes) assert "Hello from Docker".encode() in stdout, "There should be something on stdout" + + +def test_docker_container_with_env_file(): + """Test that environment variables can be loaded from a file""" + with tempfile.TemporaryDirectory() as temp_directory: + env_file_path = Path(temp_directory) / "env_file" + with open(env_file_path, "w") as f: + f.write( + """ + TEST_ENV_VAR=hello + NUMBER=123 + DOMAIN=example.org + ADMIN_EMAIL=admin@${DOMAIN} + ROOT_URL=${DOMAIN}/app + """ + ) + container = DockerContainer("alpine").with_command("tail -f /dev/null") # Keep the container running + container.with_env_file(env_file_path) # Load the environment variables from the file + with container: + output = container.exec("env").output.decode("utf-8").strip() + assert "TEST_ENV_VAR=hello" in output + assert "NUMBER=123" in output + assert "DOMAIN=example.org" in output + assert "ADMIN_EMAIL=admin@example.org" in output + assert "ROOT_URL=example.org/app" in output + print(output) diff --git a/poetry.lock b/poetry.lock index b83de36f..bd54659e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -4678,4 +4678,4 @@ weaviate = ["weaviate-client"] [metadata] lock-version = "2.0" python-versions = ">=3.9,<4.0" -content-hash = "7ffcf39257e1ac79d951b7ccb2cdb972beaaef35d0cd1f722d2964c5bdced674" +content-hash = "5c400cc87dc9708588ee8d7d50646de789235732d868b74ebc43f1cf2a403c88" diff --git a/pyproject.toml b/pyproject.toml index bd4e7ed0..a44cc3a3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -83,6 +83,7 @@ docker = "*" # ">=4.0" urllib3 = "*" # "<2.0" wrapt = "*" # "^1.16.0" typing-extensions = "*" +python-dotenv = "*" # community modules python-arango = { version = "^7.8", optional = true }