From 5d4f0325756304f6b52bde6924f00c9c98f92ad3 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Fri, 13 Oct 2023 07:53:41 +0900 Subject: [PATCH] nerdctl compose: fix --env-file Fix issue 2566 This was a regression in PR 2472 (v1.6.0) Signed-off-by: Akihiro Suda --- cmd/nerdctl/compose_config_test.go | 21 +++++++++++++++++++++ pkg/composer/composer.go | 10 ++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/cmd/nerdctl/compose_config_test.go b/cmd/nerdctl/compose_config_test.go index 4f519a46c99..705d467cb8f 100644 --- a/cmd/nerdctl/compose_config_test.go +++ b/cmd/nerdctl/compose_config_test.go @@ -130,3 +130,24 @@ services: base.ComposeCmd("--project-directory", comp.Dir(), "config", "--services").AssertOutContainsAll("hello1\n", "hello2\n") base.ComposeCmd("--project-directory", comp.Dir(), "config").AssertOutContains("alpine:3.14") } + +func TestComposeConfigWithEnvFile(t *testing.T) { + base := testutil.NewBase(t) + + const dockerComposeYAML = ` +services: + hello: + image: ${image} +` + + comp := testutil.NewComposeDir(t, dockerComposeYAML) + defer comp.CleanUp() + + envFile := filepath.Join(comp.Dir(), "env") + const envFileContent = ` +image: hello-world +` + assert.NilError(t, os.WriteFile(envFile, []byte(envFileContent), 0644)) + + base.ComposeCmd("-f", comp.YAMLFullPath(), "--env-file", envFile, "config").AssertOutContains("image: hello-world") +} diff --git a/pkg/composer/composer.go b/pkg/composer/composer.go index a01fc41576e..67a533dc2ac 100644 --- a/pkg/composer/composer.go +++ b/pkg/composer/composer.go @@ -70,16 +70,18 @@ func New(o Options, client *containerd.Client) (*Composer, error) { optionsFn = append(optionsFn, composecli.WithOsEnv, composecli.WithWorkingDirectory(o.ProjectDirectory), - composecli.WithConfigFileEnv, - composecli.WithDefaultConfigPath, - composecli.WithDotEnv, - composecli.WithName(o.Project), ) if o.EnvFile != "" { optionsFn = append(optionsFn, composecli.WithEnvFiles(o.EnvFile), ) } + optionsFn = append(optionsFn, + composecli.WithConfigFileEnv, + composecli.WithDefaultConfigPath, + composecli.WithDotEnv, + composecli.WithName(o.Project), + ) projectOptions, err := composecli.NewProjectOptions(o.ConfigPaths, optionsFn...) if err != nil {