Skip to content

Commit

Permalink
allow a local .env file to override compose.yaml sibling .env
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed May 16, 2024
1 parent eb5e018 commit 16622fc
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
26 changes: 18 additions & 8 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func RootCommand(dockerCli command.Cli, backend Backend) *cobra.Command { //noli
ctx := cmd.Context()

// (1) process env vars
err := setEnvWithDotEnv(&opts)
err := setEnvWithLocalDotEnv(&opts)
if err != nil {
return err
}
Expand Down Expand Up @@ -594,18 +594,28 @@ func RootCommand(dockerCli command.Cli, backend Backend) *cobra.Command { //noli
return c
}

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

envFromFile, err := dotenv.GetEnvFromFile(composegoutils.GetAsEqualsMap(os.Environ()), options.EnvFiles)
defaultDotEnv := filepath.Join(wd, ".env")

s, err := os.Stat(defaultDotEnv)
if os.IsNotExist(err) || s.IsDir() {
return nil
}
if err != nil {
return err
}

envFromFile, err := dotenv.GetEnvFromFile(composegoutils.GetAsEqualsMap(os.Environ()), []string{defaultDotEnv})
if err != nil {
return err
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/e2e/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,24 @@ func TestResolveDotEnv(t *testing.T) {
Out: "image: backend:latest",
})
}

func TestNestedDotEnv(t *testing.T) {
c := NewCLI(t)

cmd := c.NewDockerComposeCmd(t, "run", "echo")
cmd.Dir = filepath.Join(".", "fixtures", "nested")
res := icmd.RunCmd(cmd)
res.Assert(t, icmd.Expected{
ExitCode: 0,
Out: "root win=root",
})

cmd = c.NewDockerComposeCmd(t, "run", "echo")
cmd.Dir = filepath.Join(".", "fixtures", "nested", "sub")
res = icmd.RunCmd(cmd)
res.Assert(t, icmd.Expected{
ExitCode: 0,
Out: "root sub win=sub",
})

}
2 changes: 2 additions & 0 deletions pkg/e2e/fixtures/nested/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ROOT=root
WIN=root
4 changes: 4 additions & 0 deletions pkg/e2e/fixtures/nested/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
echo:
image: alpine
command: echo $ROOT $SUB win=$WIN
2 changes: 2 additions & 0 deletions pkg/e2e/fixtures/nested/sub/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SUB=sub
WIN=sub

0 comments on commit 16622fc

Please sign in to comment.