From b0921b453b611e06919ff6ba0ebd5c4795e0109a Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Fri, 29 Nov 2024 21:27:10 +0000 Subject: [PATCH 1/2] fix: Fix --env-file flag behavior in nerdctl compose Signed-off-by: Arjun Raja Yogidas --- pkg/composer/up_service.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/composer/up_service.go b/pkg/composer/up_service.go index f3f9b26136d..5444408f092 100644 --- a/pkg/composer/up_service.go +++ b/pkg/composer/up_service.go @@ -157,6 +157,10 @@ func (c *Composer) upServiceContainer(ctx context.Context, service *serviceparse runFlagD = true } + if c.EnvFile != "" { + container.RunArgs = append([]string{"--env-file=" + c.EnvFile}, container.RunArgs...) + } + //add metadata labels to container https://github.com/compose-spec/compose-spec/blob/master/spec.md#labels container.RunArgs = append([]string{ "--cidfile=" + cidFilename, From e609d83f920738c57a89d6c3cfc25d656995a6d7 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Tue, 3 Dec 2024 18:51:15 +0000 Subject: [PATCH 2/2] add test fopr compose Signed-off-by: Arjun Raja Yogidas --- cmd/nerdctl/compose/compose_up_test.go | 54 ++++++++++++++++++++++++++ docs/command-reference.md | 1 + 2 files changed, 55 insertions(+) diff --git a/cmd/nerdctl/compose/compose_up_test.go b/cmd/nerdctl/compose/compose_up_test.go index 63bba829fcf..98f16e211fd 100644 --- a/cmd/nerdctl/compose/compose_up_test.go +++ b/cmd/nerdctl/compose/compose_up_test.go @@ -83,3 +83,57 @@ services: assert.NilError(t, err) assert.Equal(t, "hi\n", string(testB)) } + +func TestComposeUpEnvFile(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("Skipping test on Windows") + } + + base := testutil.NewBase(t) + + // Create a temporary directory for the test + tmpDir := t.TempDir() + fmt.Printf("Created temporary directory: %s\n", tmpDir) + + // Create an .env file + envFilePath := filepath.Join(tmpDir, ".env") + envFileContent := ` +TEST_VAR1=Hello +TEST_VAR2=World +` + err := os.WriteFile(envFilePath, []byte(envFileContent), 0644) + assert.NilError(t, err) + fmt.Printf("Created .env file at: %s\n", envFilePath) + fmt.Printf("Env file content:\n%s\n", envFileContent) + + // Create docker-compose.yml + dockerComposeYAML := fmt.Sprintf(` +version: '3' +services: + test: + image: %s + command: sh -c 'echo $TEST_VAR1 $TEST_VAR2 > /tmp/test_output' +`, testutil.CommonImage) + + comp := testutil.NewComposeDir(t, dockerComposeYAML) + defer comp.CleanUp() + fmt.Printf("Created docker-compose.yml at: %s\n", comp.YAMLFullPath()) + fmt.Printf("Docker Compose YAML content:\n%s\n", dockerComposeYAML) + + // Run compose up with env-file + upCmd := base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "--env-file", envFilePath) + upCmd.AssertOK() + defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down").AssertOK() + + // Check if the environment variables were correctly passed and used + containerID := base.ComposeCmd("-f", comp.YAMLFullPath(), "ps", "-q").OutLines()[0] + fmt.Printf("Container ID: %s\n", containerID) + + execCmd := base.Cmd("exec", containerID, "cat", "/tmp/test_output") + out := execCmd.Out() + + fmt.Printf("Command output: %s\n", out) + + assert.Equal(t, "Hello World\n", out) + fmt.Println("Test completed successfully") +} diff --git a/docs/command-reference.md b/docs/command-reference.md index 95dfddcfd45..6f4dafeb542 100644 --- a/docs/command-reference.md +++ b/docs/command-reference.md @@ -1391,6 +1391,7 @@ Flags: - :whale: `-p, --project-name`: Specify an alternate project name - :nerd_face: `--ipfs-address`: Multiaddr of IPFS API (default uses `$IPFS_PATH` env variable if defined or local directory `~/.ipfs`) - :whale: `--profile: Specify a profile to enable +- :whale: `--env-file` : Specify an alternate environment file ### :whale: nerdctl compose up