Skip to content

Commit 6312061

Browse files
authored
Add envs option to docker.compose.run() (#664) (#665)
1 parent 406eea4 commit 6312061

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

python_on_whales/components/compose/cli_wrapper.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ def run(
665665
build: bool = False,
666666
detach: bool = False,
667667
entrypoint: Optional[str] = None,
668-
# envs: Dict[str, str] = {},
668+
envs: Dict[str, str] = {},
669669
labels: Dict[str, str] = {},
670670
name: Optional[str] = None,
671671
tty: bool = True,
@@ -695,6 +695,7 @@ def run(
695695
detach: if `True`, returns immediately with the Container.
696696
If `False`, returns the command stdout as string.
697697
entrypoint: The entrypoint to execute.
698+
envs: A dictionary of environment variables to set in the container.
698699
labels: Add or override labels
699700
name: Assign a name to the container.
700701
dependencies: Also start linked services.
@@ -735,6 +736,8 @@ def run(
735736
full_cmd.add_flag("--build", build)
736737
full_cmd.add_flag("--detach", detach)
737738
full_cmd.add_simple_arg("--entrypoint", entrypoint)
739+
for key, value in envs.items():
740+
full_cmd.add_simple_arg("--env", f"{key}={value}")
738741
full_cmd.add_simple_arg("--name", name)
739742
full_cmd.add_flag("--no-TTY", not tty)
740743
full_cmd.add_flag("--no-deps", not dependencies)

tests/python_on_whales/components/test_compose.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,18 @@ def test_compose_run_entrypoint():
765765
assert result == "cmd-is-argument-for-echo"
766766

767767

768+
def test_compose_run_envs():
769+
result = docker.compose.run(
770+
"dodo",
771+
["-c", "echo $VAR1 $VAR2"],
772+
envs={"VAR1": "hello", "VAR2": "world"},
773+
remove=True,
774+
tty=False,
775+
)
776+
777+
assert result == "hello world"
778+
779+
768780
def test_compose_run_volume():
769781
with tempfile.NamedTemporaryFile() as fst, tempfile.NamedTemporaryFile() as snd:
770782
fst.write(b"Hello ")

0 commit comments

Comments
 (0)