1
0
Эх сурвалжийг харах

run only loads required service env_file and ignores others

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 8 сар өмнө
parent
commit
6a8c0988cf

+ 6 - 1
cmd/compose/run.go

@@ -184,7 +184,12 @@ func runCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *
 			return nil
 		}),
 		RunE: Adapt(func(ctx context.Context, args []string) error {
-			project, _, err := p.ToProject(ctx, dockerCli, []string{options.Service}, cgo.WithResolvedPaths(true), cgo.WithDiscardEnvFile)
+			project, _, err := p.ToProject(ctx, dockerCli, []string{options.Service}, cgo.WithResolvedPaths(true), cgo.WithoutEnvironmentResolution)
+			if err != nil {
+				return err
+			}
+
+			project, err = project.WithServicesEnvironmentResolved(true)
 			if err != nil {
 				return err
 			}

+ 9 - 0
pkg/e2e/env_file_test.go

@@ -21,6 +21,7 @@ import (
 	"testing"
 
 	"gotest.tools/v3/assert"
+	"gotest.tools/v3/icmd"
 )
 
 func TestRawEnvFile(t *testing.T) {
@@ -37,3 +38,11 @@ func TestUnusedMissingEnvFile(t *testing.T) {
 
 	c.RunDockerComposeCmd(t, "-f", "./fixtures/env_file/compose.yaml", "up", "-d", "serviceA")
 }
+
+func TestRunEnvFile(t *testing.T) {
+	c := NewParallelCLI(t)
+	defer c.cleanupWithDown(t, "run_dotenv")
+
+	res := c.RunDockerComposeCmd(t, "--project-directory", "./fixtures/env_file", "run", "serviceC", "env")
+	res.Assert(t, icmd.Expected{Out: "FOO=BAR"})
+}

+ 7 - 1
pkg/e2e/fixtures/env_file/compose.yaml

@@ -5,4 +5,10 @@ services:
   serviceB:
     image: nginx:latest
     env_file:
-      - /doesnotexist/.env
+      - /doesnotexist/.env
+
+  serviceC:
+    profiles: ["test"]
+    image: alpine
+    env_file: test.env
+

+ 1 - 0
pkg/e2e/fixtures/env_file/test.env

@@ -0,0 +1 @@
+FOO=BAR