diff --git a/python_on_whales/components/compose/cli_wrapper.py b/python_on_whales/components/compose/cli_wrapper.py index 2f4e1009..1ccef976 100644 --- a/python_on_whales/components/compose/cli_wrapper.py +++ b/python_on_whales/components/compose/cli_wrapper.py @@ -58,7 +58,7 @@ def build( full_cmd += services run(full_cmd, capture_stdout=False) - def config(self, return_json: bool = False) -> Union[ComposeConfig, Dict[str, Any]]: + def config(self, return_json: bool = False, envs: Dict[str,str] = {}) -> Union[ComposeConfig, Dict[str, Any]]: """Returns the configuration of the compose stack for further inspection. For example @@ -76,12 +76,13 @@ def config(self, return_json: bool = False) -> Union[ComposeConfig, Dict[str, An lists and dicts corresponding to the json response, unmodified. It may be useful if you just want to print the config or want to access a field that was not in the `ComposeConfig` class. + envs: A dictionary of environment variables to set for the compose process. # Returns A `ComposeConfig` object if `return_json` is `False`, and a `dict` otherwise. """ full_cmd = self.docker_compose_cmd + ["config", "--format", "json"] - result = run(full_cmd, capture_stdout=True) + result = run(full_cmd, capture_stdout=True, env=envs) if return_json: return json.loads(result) else: @@ -651,6 +652,7 @@ def up( log_prefix: bool = True, start: bool = True, quiet: bool = False, + envs: Dict[str, str] = {}, ): """Start the containers. @@ -681,6 +683,7 @@ def up( start: Start the service after creating them. quiet: By default, some progress bars and logs are sent to stderr and stdout. Set `quiet=True` to avoid having any output. + envs: A dictionary of environment variables to set for the compose process. # Returns `None` at the moment. The plan is to be able to capture and stream the logs later. @@ -706,7 +709,7 @@ def up( services = to_list(services) full_cmd += services # important information is written to both stdout AND stderr. - run(full_cmd, capture_stdout=quiet, capture_stderr=quiet) + run(full_cmd, capture_stdout=quiet, capture_stderr=quiet, env=envs) def version(self) -> str: """Returns the version of docker compose as a `str`.""" diff --git a/tests/python_on_whales/components/dummy_compose.yml b/tests/python_on_whales/components/dummy_compose.yml index 374b7e25..ca1d7d3c 100644 --- a/tests/python_on_whales/components/dummy_compose.yml +++ b/tests/python_on_whales/components/dummy_compose.yml @@ -15,6 +15,8 @@ services: busybox: image: busybox:latest command: sleep infinity + environment: + - SOME_VARIABLE=${SOME_VARIABLE_TO_INSERT:-nothing} busybox-2-electric-boogaloo: image: busybox:latest depends_on: diff --git a/tests/python_on_whales/components/test_compose.py b/tests/python_on_whales/components/test_compose.py index d49a9733..e9852d4d 100644 --- a/tests/python_on_whales/components/test_compose.py +++ b/tests/python_on_whales/components/test_compose.py @@ -218,6 +218,22 @@ def test_docker_compose_up_down_some_services(): docker.compose.down(timeout=1) +def test_docker_compose_config_envs(tmp_path: Path): + output = docker.compose.config(envs={"SOME_VARIABLE_TO_INSERT": "test-value"}) + assert output.services["busybox"].environment["SOME_VARIABLE"] == "test-value" + + +def test_docker_compose_up_envs(tmp_path: Path): + docker.compose.up(["busybox"], detach=True, envs={"SOME_VARIABLE_TO_INSERT": "hello world"},) + output = docker.compose.execute( + "busybox", + ["sh", "-c", "echo $SOME_VARIABLE"], + tty=False, + ) + assert output == "hello world" + docker.compose.down(timeout=1) + + def test_docker_compose_ps(): docker.compose.up(["my_service", "busybox"], detach=True) containers = docker.compose.ps()