Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions cmd/nerdctl/compose/compose_up_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
1 change: 1 addition & 0 deletions docs/command-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions pkg/composer/up_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading