@@ -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 :
@@ -485,7 +490,7 @@ def run(
485490 command : List [str ] = [],
486491 detach : bool = False ,
487492 # entrypoint: Optional[List[str]] = None,
488- # envs: Dict[str, str] = {},
493+ envs : Dict [str , str ] = {},
489494 # labels: Dict[str, str] = {},
490495 name : Optional [str ] = None ,
491496 tty : bool = True ,
@@ -567,10 +572,12 @@ def run(
567572 full_cmd .append (service )
568573 full_cmd += command
569574
575+ run_envs = {** self .docker_compose_envs , ** envs }
576+
570577 if stream :
571578 return stream_stdout_and_stderr (full_cmd )
572579 else :
573- result = run (full_cmd , tty = tty )
580+ result = run (full_cmd , tty = tty , env = run_envs )
574581 if detach :
575582 Container = python_on_whales .components .container .cli_wrapper .Container
576583 return Container (self .client_config , result , is_immutable_id = True )
@@ -651,6 +658,7 @@ def up(
651658 log_prefix : bool = True ,
652659 start : bool = True ,
653660 quiet : bool = False ,
661+ envs : Dict [str , str ] = {},
654662 ):
655663 """Start the containers.
656664
@@ -681,6 +689,7 @@ def up(
681689 start: Start the service after creating them.
682690 quiet: By default, some progress bars and logs are sent to stderr and stdout.
683691 Set `quiet=True` to avoid having any output.
692+ envs: A dictionary of environment variables to set for the compose process.
684693
685694 # Returns
686695 `None` at the moment. The plan is to be able to capture and stream the logs later.
@@ -700,13 +709,15 @@ def up(
700709 full_cmd .add_flag ("--no-log-prefix" , not log_prefix )
701710 full_cmd .add_flag ("--no-start" , not start )
702711
712+ run_envs = {** self .docker_compose_envs , ** envs }
713+
703714 if services == []:
704715 return
705716 elif services is not None :
706717 services = to_list (services )
707718 full_cmd += services
708719 # important information is written to both stdout AND stderr.
709- run (full_cmd , capture_stdout = quiet , capture_stderr = quiet )
720+ run (full_cmd , capture_stdout = quiet , capture_stderr = quiet , env = run_envs )
710721
711722 def version (self ) -> str :
712723 """Returns the version of docker compose as a `str`."""
0 commit comments