@@ -247,6 +247,11 @@ def __init__(self, project, options=None):
247247 def project_dir (self ):
248248 return self .toplevel_options .get ('--project-directory' ) or '.'
249249
250+ @property
251+ def toplevel_environment (self ):
252+ environment_file = self .toplevel_options .get ('--env-file' )
253+ return Environment .from_env_file (self .project_dir , environment_file )
254+
250255 def build (self , options ):
251256 """
252257 Build or rebuild services.
@@ -276,9 +281,7 @@ def build(self, options):
276281 '--build-arg is only supported when services are specified for API version < 1.25.'
277282 ' Please use a Compose file version > 2.2 or specify which services to build.'
278283 )
279- environment_file = options .get ('--env-file' )
280- environment = Environment .from_env_file (self .project_dir , environment_file )
281- build_args = resolve_build_args (build_args , environment )
284+ build_args = resolve_build_args (build_args , self .toplevel_environment )
282285
283286 self .project .build (
284287 service_names = options ['SERVICE' ],
@@ -429,11 +432,8 @@ def down(self, options):
429432 Compose file
430433 -t, --timeout TIMEOUT Specify a shutdown timeout in seconds.
431434 (default: 10)
432- --env-file PATH Specify an alternate environment file
433435 """
434- environment_file = options .get ('--env-file' )
435- environment = Environment .from_env_file (self .project_dir , environment_file )
436- ignore_orphans = environment .get_boolean ('COMPOSE_IGNORE_ORPHANS' )
436+ ignore_orphans = self .toplevel_environment .get_boolean ('COMPOSE_IGNORE_ORPHANS' )
437437
438438 if ignore_orphans and options ['--remove-orphans' ]:
439439 raise UserError ("COMPOSE_IGNORE_ORPHANS and --remove-orphans cannot be combined." )
@@ -489,11 +489,8 @@ def exec_command(self, options):
489489 -e, --env KEY=VAL Set environment variables (can be used multiple times,
490490 not supported in API < 1.25)
491491 -w, --workdir DIR Path to workdir directory for this command.
492- --env-file PATH Specify an alternate environment file
493492 """
494- environment_file = options .get ('--env-file' )
495- environment = Environment .from_env_file (self .project_dir , environment_file )
496- use_cli = not environment .get_boolean ('COMPOSE_INTERACTIVE_NO_CLI' )
493+ use_cli = not self .toplevel_environment .get_boolean ('COMPOSE_INTERACTIVE_NO_CLI' )
497494 index = int (options .get ('--index' ))
498495 service = self .project .get_service (options ['SERVICE' ])
499496 detach = options .get ('--detach' )
@@ -516,7 +513,7 @@ def exec_command(self, options):
516513 if IS_WINDOWS_PLATFORM or use_cli and not detach :
517514 sys .exit (call_docker (
518515 build_exec_command (options , container .id , command ),
519- self .toplevel_options , environment )
516+ self .toplevel_options , self . toplevel_environment )
520517 )
521518
522519 create_exec_options = {
@@ -890,7 +887,7 @@ def run(self, options):
890887 container_options = build_one_off_container_options (options , detach , command )
891888 run_one_off_container (
892889 container_options , self .project , service , options ,
893- self .toplevel_options , self .project_dir
890+ self .toplevel_options , self .toplevel_environment
894891 )
895892
896893 def scale (self , options ):
@@ -1051,7 +1048,6 @@ def up(self, options):
10511048 container. Implies --abort-on-container-exit.
10521049 --scale SERVICE=NUM Scale SERVICE to NUM instances. Overrides the
10531050 `scale` setting in the Compose file if present.
1054- --env-file PATH Specify an alternate environment file
10551051 """
10561052 start_deps = not options ['--no-deps' ]
10571053 always_recreate_deps = options ['--always-recreate-deps' ]
@@ -1066,9 +1062,7 @@ def up(self, options):
10661062 if detached and (cascade_stop or exit_value_from ):
10671063 raise UserError ("--abort-on-container-exit and -d cannot be combined." )
10681064
1069- environment_file = options .get ('--env-file' )
1070- environment = Environment .from_env_file (self .project_dir , environment_file )
1071- ignore_orphans = environment .get_boolean ('COMPOSE_IGNORE_ORPHANS' )
1065+ ignore_orphans = self .toplevel_environment .get_boolean ('COMPOSE_IGNORE_ORPHANS' )
10721066
10731067 if ignore_orphans and remove_orphans :
10741068 raise UserError ("COMPOSE_IGNORE_ORPHANS and --remove-orphans cannot be combined." )
@@ -1331,7 +1325,7 @@ def build_one_off_container_options(options, detach, command):
13311325
13321326
13331327def run_one_off_container (container_options , project , service , options , toplevel_options ,
1334- project_dir = '.' ):
1328+ toplevel_environment ):
13351329 if not options ['--no-deps' ]:
13361330 deps = service .get_dependency_names ()
13371331 if deps :
@@ -1360,9 +1354,8 @@ def remove_container(force=False):
13601354 if options ['--rm' ]:
13611355 project .client .remove_container (container .id , force = True , v = True )
13621356
1363- environment_file = options .get ('--env-file' )
1364- environment = Environment .from_env_file (project_dir , environment_file )
1365- use_cli = not environment .get_boolean ('COMPOSE_INTERACTIVE_NO_CLI' )
1357+ use_cli = not toplevel_environment .get_boolean ('COMPOSE_INTERACTIVE_NO_CLI' )
1358+
13661359 signals .set_signal_handler_to_shutdown ()
13671360 signals .set_signal_handler_to_hang_up ()
13681361 try :
@@ -1371,7 +1364,7 @@ def remove_container(force=False):
13711364 service .connect_container_to_networks (container , use_network_aliases )
13721365 exit_code = call_docker (
13731366 get_docker_start_call (container_options , container .id ),
1374- toplevel_options , environment
1367+ toplevel_options , toplevel_environment
13751368 )
13761369 else :
13771370 operation = RunOperation (
0 commit comments