Skip to content

Commit 9dd081b

Browse files
gloursndeloof
authored andcommitted
add support of COMPOSE_ENV_FILES env variable to pass a list of env files
Signed-off-by: Guillaume Lours <[email protected]>
1 parent 2c0b023 commit 9dd081b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

cmd/compose/compose.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ const (
6060
ComposeRemoveOrphans = "COMPOSE_REMOVE_ORPHANS"
6161
// ComposeIgnoreOrphans ignore "orphaned" containers
6262
ComposeIgnoreOrphans = "COMPOSE_IGNORE_ORPHANS"
63+
// ComposeEnvFiles defines the env files to use if --env-file isn't used
64+
ComposeEnvFiles = "COMPOSE_ENV_FILES"
6365
)
6466

6567
// Command defines a compose CLI command as a func with args
@@ -517,6 +519,11 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command { //
517519
}
518520

519521
func setEnvWithDotEnv(prjOpts *ProjectOptions) error {
522+
if len(prjOpts.EnvFiles) == 0 {
523+
if envFiles := os.Getenv(ComposeEnvFiles); envFiles != "" {
524+
prjOpts.EnvFiles = strings.Split(envFiles, ",")
525+
}
526+
}
520527
options, err := prjOpts.toProjectOptions()
521528
if err != nil {
522529
return compose.WrapComposeError(err)

pkg/e2e/compose_environment_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ func TestEnvPriority(t *testing.T) {
106106
assert.Equal(t, strings.TrimSpace(res.Stdout()), "EnvFileDefaultValue")
107107
})
108108

109+
// No Compose file, all other options with env variable from OS environment
110+
// 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected (From environment default value from file in COMPOSE_ENV_FILES)
111+
// 2. Compose File (service::environment section)
112+
// 3. Compose File (service::env_file section file)
113+
// 4. Container Image ENV directive
114+
// 5. Variable is not defined
115+
t.Run("shell priority from COMPOSE_ENV_FILES variable", func(t *testing.T) {
116+
cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml",
117+
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
118+
cmd.Env = append(cmd.Env, "COMPOSE_ENV_FILES=./fixtures/environment/env-priority/.env.override.with.default")
119+
res := icmd.RunCmd(cmd)
120+
assert.Equal(t, strings.TrimSpace(res.Stdout()), "EnvFileDefaultValue")
121+
})
122+
109123
// No Compose file and env variable pass to the run command
110124
// 1. Command Line (docker compose run --env <KEY[=VAL]>) <-- Result expected
111125
// 2. Compose File (service::environment section)

0 commit comments

Comments
 (0)