Skip to content

Commit 1873276

Browse files
committed
allow a local .env file to override compose.yaml sibling .env
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent eb5e018 commit 1873276

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

cmd/compose/compose.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ func RootCommand(dockerCli command.Cli, backend Backend) *cobra.Command { //noli
384384
ctx := cmd.Context()
385385

386386
// (1) process env vars
387-
err := setEnvWithDotEnv(&opts)
387+
err := setEnvWithLocalDotEnv(&opts)
388388
if err != nil {
389389
return err
390390
}
@@ -594,18 +594,28 @@ func RootCommand(dockerCli command.Cli, backend Backend) *cobra.Command { //noli
594594
return c
595595
}
596596

597-
func setEnvWithDotEnv(prjOpts *ProjectOptions) error {
598-
if len(prjOpts.EnvFiles) == 0 {
599-
if envFiles := os.Getenv(ComposeEnvFiles); envFiles != "" {
600-
prjOpts.EnvFiles = strings.Split(envFiles, ",")
601-
}
597+
// If user has a local .env file, load it as os.environment so it can be used to set COMPOSE_ variables
598+
// This also allows to override values set by the default .env in a compose project when ran from a distinct folder
599+
func setEnvWithLocalDotEnv(prjOpts *ProjectOptions) error {
600+
if len(prjOpts.EnvFiles) > 0 {
601+
return nil
602602
}
603-
options, err := prjOpts.toProjectOptions()
603+
wd, err := os.Getwd()
604604
if err != nil {
605605
return compose.WrapComposeError(err)
606606
}
607607

608-
envFromFile, err := dotenv.GetEnvFromFile(composegoutils.GetAsEqualsMap(os.Environ()), options.EnvFiles)
608+
defaultDotEnv := filepath.Join(wd, ".env")
609+
610+
s, err := os.Stat(defaultDotEnv)
611+
if os.IsNotExist(err) || s.IsDir() {
612+
return nil
613+
}
614+
if err != nil {
615+
return err
616+
}
617+
618+
envFromFile, err := dotenv.GetEnvFromFile(composegoutils.GetAsEqualsMap(os.Environ()), []string{defaultDotEnv})
609619
if err != nil {
610620
return err
611621
}

pkg/e2e/compose_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,15 @@ func TestResolveDotEnv(t *testing.T) {
329329
Out: "image: backend:latest",
330330
})
331331
}
332+
333+
func TestNestedDotEnv(t *testing.T) {
334+
c := NewCLI(t)
335+
336+
cmd := c.NewDockerComposeCmd(t, "run", "echo")
337+
cmd.Dir = filepath.Join(".", "fixtures", "nested")
338+
res := icmd.RunCmd(cmd)
339+
res.Assert(t, icmd.Expected{
340+
ExitCode: 0,
341+
Out: "root sub win=sub",
342+
})
343+
}

pkg/e2e/fixtures/nested/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ROOT=root
2+
WIN=root
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
services:
2+
echo:
3+
image: alpine
4+
command: echo $ROOT $SUB win=$WIN

pkg/e2e/fixtures/nested/sub/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SUB=sub
2+
WIN=sub

0 commit comments

Comments
 (0)