Skip to content

Commit 87fa696

Browse files
committed
Add envs parameter to compose up/config
1 parent 7dc3102 commit 87fa696

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

python_on_whales/components/compose/cli_wrapper.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ def build(
5858
full_cmd += services
5959
run(full_cmd, capture_stdout=False)
6060

61-
def config(self, return_json: bool = False) -> Union[ComposeConfig, Dict[str, Any]]:
61+
def config(
62+
self, return_json: bool = False, envs: Dict[str, str] = {}
63+
) -> Union[ComposeConfig, Dict[str, Any]]:
6264
"""Returns the configuration of the compose stack for further inspection.
6365
6466
For example
@@ -76,12 +78,13 @@ def config(self, return_json: bool = False) -> Union[ComposeConfig, Dict[str, An
7678
lists and dicts corresponding to the json response, unmodified.
7779
It may be useful if you just want to print the config or want to access
7880
a field that was not in the `ComposeConfig` class.
81+
envs: A dictionary of environment variables to set for the compose process.
7982
8083
# Returns
8184
A `ComposeConfig` object if `return_json` is `False`, and a `dict` otherwise.
8285
"""
8386
full_cmd = self.docker_compose_cmd + ["config", "--format", "json"]
84-
result = run(full_cmd, capture_stdout=True)
87+
result = run(full_cmd, capture_stdout=True, env=envs)
8588
if return_json:
8689
return json.loads(result)
8790
else:
@@ -651,6 +654,7 @@ def up(
651654
log_prefix: bool = True,
652655
start: bool = True,
653656
quiet: bool = False,
657+
envs: Dict[str, str] = {},
654658
):
655659
"""Start the containers.
656660
@@ -681,6 +685,7 @@ def up(
681685
start: Start the service after creating them.
682686
quiet: By default, some progress bars and logs are sent to stderr and stdout.
683687
Set `quiet=True` to avoid having any output.
688+
envs: A dictionary of environment variables to set for the compose process.
684689
685690
# Returns
686691
`None` at the moment. The plan is to be able to capture and stream the logs later.
@@ -706,7 +711,7 @@ def up(
706711
services = to_list(services)
707712
full_cmd += services
708713
# important information is written to both stdout AND stderr.
709-
run(full_cmd, capture_stdout=quiet, capture_stderr=quiet)
714+
run(full_cmd, capture_stdout=quiet, capture_stderr=quiet, env=envs)
710715

711716
def version(self) -> str:
712717
"""Returns the version of docker compose as a `str`."""

tests/python_on_whales/components/dummy_compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ services:
1515
busybox:
1616
image: busybox:latest
1717
command: sleep infinity
18+
environment:
19+
- SOME_VARIABLE=${SOME_VARIABLE_TO_INSERT:-nothing}
1820
busybox-2-electric-boogaloo:
1921
image: busybox:latest
2022
depends_on:

tests/python_on_whales/components/test_compose.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,26 @@ def test_docker_compose_up_down_some_services():
218218
docker.compose.down(timeout=1)
219219

220220

221+
def test_docker_compose_config_envs(tmp_path: Path):
222+
output = docker.compose.config(envs={"SOME_VARIABLE_TO_INSERT": "test-value"})
223+
assert output.services["busybox"].environment["SOME_VARIABLE"] == "test-value"
224+
225+
226+
def test_docker_compose_up_envs(tmp_path: Path):
227+
docker.compose.up(
228+
["busybox"],
229+
detach=True,
230+
envs={"SOME_VARIABLE_TO_INSERT": "hello world"},
231+
)
232+
output = docker.compose.execute(
233+
"busybox",
234+
["sh", "-c", "echo $SOME_VARIABLE"],
235+
tty=False,
236+
)
237+
assert output == "hello world"
238+
docker.compose.down(timeout=1)
239+
240+
221241
def test_docker_compose_ps():
222242
docker.compose.up(["my_service", "busybox"], detach=True)
223243
containers = docker.compose.ps()

0 commit comments

Comments
 (0)