Skip to content

Commit fe206eb

Browse files
authored
feat(core): DockerCompose: support list of env_files (#847)
It is useful to be able to pass a list of `env_file`s, especially since you can pass a list of `compose_file_name`s. Builds upon #135, matching `compose_file_name`.
1 parent a072f3f commit fe206eb

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

core/testcontainers/compose/compose.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class DockerCompose:
173173
Wait for the services to be healthy
174174
(as per healthcheck definitions in the docker compose configuration)
175175
env_file:
176-
Path to an '.env' file containing environment variables
176+
Path(s) to an '.env' file containing environment variables
177177
to pass to docker compose.
178178
services:
179179
The list of services to use from this DockerCompose.
@@ -210,7 +210,7 @@ class DockerCompose:
210210
build: bool = False
211211
wait: bool = True
212212
keep_volumes: bool = False
213-
env_file: Optional[str] = None
213+
env_file: Optional[Union[str, list[str]]] = None
214214
services: Optional[list[str]] = None
215215
docker_command_path: Optional[str] = None
216216
profiles: Optional[list[str]] = None
@@ -219,6 +219,8 @@ class DockerCompose:
219219
def __post_init__(self) -> None:
220220
if isinstance(self.compose_file_name, str):
221221
self.compose_file_name = [self.compose_file_name]
222+
if isinstance(self.env_file, str):
223+
self.env_file = [self.env_file]
222224

223225
def __enter__(self) -> "DockerCompose":
224226
self.start()
@@ -247,7 +249,8 @@ def compose_command_property(self) -> list[str]:
247249
if self.profiles:
248250
docker_compose_cmd += [item for profile in self.profiles for item in ["--profile", profile]]
249251
if self.env_file:
250-
docker_compose_cmd += ["--env-file", self.env_file]
252+
for env_file in self.env_file:
253+
docker_compose_cmd += ["--env-file", env_file]
251254
return docker_compose_cmd
252255

253256
def waiting_for(self, strategies: dict[str, WaitStrategy]) -> "DockerCompose":

0 commit comments

Comments
 (0)