Skip to content

Commit 6a8c098

Browse files
ndeloofglours
authored andcommitted
run only loads required service env_file and ignores others
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 9129abe commit 6a8c098

4 files changed

Lines changed: 23 additions & 2 deletions

File tree

cmd/compose/run.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,12 @@ func runCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *
184184
return nil
185185
}),
186186
RunE: Adapt(func(ctx context.Context, args []string) error {
187-
project, _, err := p.ToProject(ctx, dockerCli, []string{options.Service}, cgo.WithResolvedPaths(true), cgo.WithDiscardEnvFile)
187+
project, _, err := p.ToProject(ctx, dockerCli, []string{options.Service}, cgo.WithResolvedPaths(true), cgo.WithoutEnvironmentResolution)
188+
if err != nil {
189+
return err
190+
}
191+
192+
project, err = project.WithServicesEnvironmentResolved(true)
188193
if err != nil {
189194
return err
190195
}

pkg/e2e/env_file_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"testing"
2222

2323
"gotest.tools/v3/assert"
24+
"gotest.tools/v3/icmd"
2425
)
2526

2627
func TestRawEnvFile(t *testing.T) {
@@ -37,3 +38,11 @@ func TestUnusedMissingEnvFile(t *testing.T) {
3738

3839
c.RunDockerComposeCmd(t, "-f", "./fixtures/env_file/compose.yaml", "up", "-d", "serviceA")
3940
}
41+
42+
func TestRunEnvFile(t *testing.T) {
43+
c := NewParallelCLI(t)
44+
defer c.cleanupWithDown(t, "run_dotenv")
45+
46+
res := c.RunDockerComposeCmd(t, "--project-directory", "./fixtures/env_file", "run", "serviceC", "env")
47+
res.Assert(t, icmd.Expected{Out: "FOO=BAR"})
48+
}

pkg/e2e/fixtures/env_file/compose.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,10 @@ services:
55
serviceB:
66
image: nginx:latest
77
env_file:
8-
- /doesnotexist/.env
8+
- /doesnotexist/.env
9+
10+
serviceC:
11+
profiles: ["test"]
12+
image: alpine
13+
env_file: test.env
14+

pkg/e2e/fixtures/env_file/test.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FOO=BAR

0 commit comments

Comments
 (0)