diff --git a/cmd/compose/run.go b/cmd/compose/run.go index 137c2526b..efc5c831a 100644 --- a/cmd/compose/run.go +++ b/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 } diff --git a/pkg/e2e/env_file_test.go b/pkg/e2e/env_file_test.go index ed7c84318..ccf02efe9 100644 --- a/pkg/e2e/env_file_test.go +++ b/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"}) +} diff --git a/pkg/e2e/fixtures/env_file/compose.yaml b/pkg/e2e/fixtures/env_file/compose.yaml index 07d5dfeb6..9983f5738 100644 --- a/pkg/e2e/fixtures/env_file/compose.yaml +++ b/pkg/e2e/fixtures/env_file/compose.yaml @@ -5,4 +5,10 @@ services: serviceB: image: nginx:latest env_file: - - /doesnotexist/.env \ No newline at end of file + - /doesnotexist/.env + + serviceC: + profiles: ["test"] + image: alpine + env_file: test.env + diff --git a/pkg/e2e/fixtures/env_file/test.env b/pkg/e2e/fixtures/env_file/test.env new file mode 100644 index 000000000..15c36f50e --- /dev/null +++ b/pkg/e2e/fixtures/env_file/test.env @@ -0,0 +1 @@ +FOO=BAR \ No newline at end of file