Skip to content

Commit 7a7c9ff

Browse files
authored
Merge pull request #6800 from KlaasH/revise-env-file-option
Make '--env-file' option top-level only and fix failure with subcommands
2 parents cd8e2f8 + 413e5db commit 7a7c9ff

5 files changed

Lines changed: 24 additions & 23 deletions

File tree

compose/cli/main.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

13331327
def 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(

compose/config/environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def split_env(env):
2626
key = env
2727
if re.search(r'\s', key):
2828
raise ConfigurationError(
29-
"environment variable name '{}' may not contains whitespace.".format(key)
29+
"environment variable name '{}' may not contain whitespace.".format(key)
3030
)
3131
return key, value
3232

contrib/completion/bash/docker-compose

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ _docker_compose_docker_compose() {
184184
_filedir -d
185185
return
186186
;;
187+
--env-file)
188+
_filedir
189+
return
190+
;;
187191
$(__docker_compose_to_extglob "$daemon_options_with_args") )
188192
return
189193
;;
@@ -612,6 +616,7 @@ _docker_compose() {
612616
--tlsverify
613617
"
614618
local daemon_options_with_args="
619+
--env-file
615620
--file -f
616621
--host -H
617622
--project-directory

contrib/completion/fish/docker-compose.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ end
1212

1313
complete -c docker-compose -s f -l file -r -d 'Specify an alternate compose file'
1414
complete -c docker-compose -s p -l project-name -x -d 'Specify an alternate project name'
15+
complete -c docker-compose -l env-file -r -d 'Specify an alternate environment file (default: .env)'
1516
complete -c docker-compose -l verbose -d 'Show more output'
1617
complete -c docker-compose -s H -l host -x -d 'Daemon socket to connect to'
1718
complete -c docker-compose -l tls -d 'Use TLS; implied by --tlsverify'

contrib/completion/zsh/_docker-compose

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ _docker-compose() {
341341
'(- :)'{-h,--help}'[Get help]' \
342342
'*'{-f,--file}"[${file_description}]:file:_files -g '*.yml'" \
343343
'(-p --project-name)'{-p,--project-name}'[Specify an alternate project name (default: directory name)]:project name:' \
344+
'--env-file[Specify an alternate environment file (default: .env)]:env-file:_files' \
344345
"--compatibility[If set, Compose will attempt to convert keys in v3 files to their non-Swarm equivalent]" \
345346
'(- :)'{-v,--version}'[Print version and exit]' \
346347
'--verbose[Show more output]' \
@@ -359,6 +360,7 @@ _docker-compose() {
359360
local -a relevant_compose_flags relevant_compose_repeatable_flags relevant_docker_flags compose_options docker_options
360361

361362
relevant_compose_flags=(
363+
"--env-file"
362364
"--file" "-f"
363365
"--host" "-H"
364366
"--project-name" "-p"

0 commit comments

Comments
 (0)