@@ -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,15 @@ 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+ run_envs = {** self .docker_compose_envs , ** envs }
88+
89+ result = run (full_cmd , capture_stdout = True , env = run_envs )
8590 if return_json :
8691 return json .loads (result )
8792 else :
@@ -490,7 +495,7 @@ def run(
490495 command : List [str ] = [],
491496 detach : bool = False ,
492497 # entrypoint: Optional[List[str]] = None,
493- # envs: Dict[str, str] = {},
498+ envs : Dict [str , str ] = {},
494499 # labels: Dict[str, str] = {},
495500 name : Optional [str ] = None ,
496501 tty : bool = True ,
@@ -572,10 +577,12 @@ def run(
572577 full_cmd .append (service )
573578 full_cmd += command
574579
580+ run_envs = {** self .docker_compose_envs , ** envs }
581+
575582 if stream :
576583 return stream_stdout_and_stderr (full_cmd )
577584 else :
578- result = run (full_cmd , tty = tty )
585+ result = run (full_cmd , tty = tty , env = run_envs )
579586 if detach :
580587 Container = python_on_whales .components .container .cli_wrapper .Container
581588 return Container (self .client_config , result , is_immutable_id = True )
@@ -656,6 +663,7 @@ def up(
656663 log_prefix : bool = True ,
657664 start : bool = True ,
658665 quiet : bool = False ,
666+ envs : Dict [str , str ] = {},
659667 ):
660668 """Start the containers.
661669
@@ -686,6 +694,7 @@ def up(
686694 start: Start the service after creating them.
687695 quiet: By default, some progress bars and logs are sent to stderr and stdout.
688696 Set `quiet=True` to avoid having any output.
697+ envs: A dictionary of environment variables to set for the compose process.
689698
690699 # Returns
691700 `None` at the moment. The plan is to be able to capture and stream the logs later.
@@ -705,13 +714,15 @@ def up(
705714 full_cmd .add_flag ("--no-log-prefix" , not log_prefix )
706715 full_cmd .add_flag ("--no-start" , not start )
707716
717+ run_envs = {** self .docker_compose_envs , ** envs }
718+
708719 if services == []:
709720 return
710721 elif services is not None :
711722 services = to_list (services )
712723 full_cmd += services
713724 # important information is written to both stdout AND stderr.
714- run (full_cmd , capture_stdout = quiet , capture_stderr = quiet )
725+ run (full_cmd , capture_stdout = quiet , capture_stderr = quiet , env = run_envs )
715726
716727 def version (self ) -> str :
717728 """Returns the version of docker compose as a `str`."""
0 commit comments