Skip to content

Commit e3dd810

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

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

python_on_whales/components/compose/cli_wrapper.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ 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(self, return_json: bool = False, envs: Dict[str,str] = {}) -> Union[ComposeConfig, Dict[str, Any]]:
6262
"""Returns the configuration of the compose stack for further inspection.
6363
6464
For example
@@ -76,12 +76,13 @@ def config(self, return_json: bool = False) -> Union[ComposeConfig, Dict[str, An
7676
lists and dicts corresponding to the json response, unmodified.
7777
It may be useful if you just want to print the config or want to access
7878
a field that was not in the `ComposeConfig` class.
79+
envs: A dictionary of environment variables to set for the compose process.
7980
8081
# Returns
8182
A `ComposeConfig` object if `return_json` is `False`, and a `dict` otherwise.
8283
"""
8384
full_cmd = self.docker_compose_cmd + ["config", "--format", "json"]
84-
result = run(full_cmd, capture_stdout=True)
85+
result = run(full_cmd, capture_stdout=True, env=envs)
8586
if return_json:
8687
return json.loads(result)
8788
else:
@@ -651,6 +652,7 @@ def up(
651652
log_prefix: bool = True,
652653
start: bool = True,
653654
quiet: bool = False,
655+
envs: Dict[str, str] = {},
654656
):
655657
"""Start the containers.
656658
@@ -681,6 +683,7 @@ def up(
681683
start: Start the service after creating them.
682684
quiet: By default, some progress bars and logs are sent to stderr and stdout.
683685
Set `quiet=True` to avoid having any output.
686+
envs: A dictionary of environment variables to set for the compose process.
684687
685688
# Returns
686689
`None` at the moment. The plan is to be able to capture and stream the logs later.
@@ -706,7 +709,7 @@ def up(
706709
services = to_list(services)
707710
full_cmd += services
708711
# important information is written to both stdout AND stderr.
709-
run(full_cmd, capture_stdout=quiet, capture_stderr=quiet)
712+
run(full_cmd, capture_stdout=quiet, capture_stderr=quiet, env=envs)
710713

711714
def version(self) -> str:
712715
"""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)