Skip to content

Commit 8b06f2a

Browse files
committed
Fix env_file in compose work with profile
Signed-off-by: Kay Yan <[email protected]>
1 parent 7efb818 commit 8b06f2a

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

Diff for: cmd/nerdctl/compose/compose_up_linux_test.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package compose
1919
import (
2020
"fmt"
2121
"io"
22+
"os"
2223
"strings"
2324
"testing"
2425
"time"
@@ -499,6 +500,12 @@ func TestComposeUpProfile(t *testing.T) {
499500
serviceRegular := testutil.Identifier(t) + "-regular"
500501
serviceProfiled := testutil.Identifier(t) + "-profiled"
501502

503+
// write the env.common file to tmpdir
504+
tmpDir := t.TempDir()
505+
envFilePath := fmt.Sprintf("%s/env.common", tmpDir)
506+
err := os.WriteFile(envFilePath, []byte("TEST_ENV_INJECTION=WORKS\n"), 0644)
507+
assert.NilError(t, err)
508+
502509
dockerComposeYAML := fmt.Sprintf(`
503510
services:
504511
%s:
@@ -508,7 +515,9 @@ services:
508515
image: %[3]s
509516
profiles:
510517
- test-profile
511-
`, serviceRegular, serviceProfiled, testutil.NginxAlpineImage)
518+
env_file:
519+
- %[4]s
520+
`, serviceRegular, serviceProfiled, testutil.NginxAlpineImage, envFilePath)
512521

513522
// * Test with profile
514523
// Should run both the services:
@@ -521,6 +530,10 @@ services:
521530
psCmd := base.Cmd("ps", "-a", "--format={{.Names}}")
522531
psCmd.AssertOutContains(serviceRegular)
523532
psCmd.AssertOutContains(serviceProfiled)
533+
534+
execCmd := base.ComposeCmd("-f", comp1.YAMLFullPath(), "exec", serviceProfiled, "env")
535+
execCmd.AssertOutContains("TEST_ENV_INJECTION=WORKS")
536+
524537
base.ComposeCmd("-f", comp1.YAMLFullPath(), "--profile", "test-profile", "down", "-v").AssertOK()
525538

526539
// * Test without profile

Diff for: pkg/composer/composer.go

+1-13
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func New(o Options, client *containerd.Client) (*Composer, error) {
8484
composecli.WithEnvFiles(),
8585
composecli.WithDotEnv,
8686
composecli.WithName(o.Project),
87+
composecli.WithProfiles(o.Profiles),
8788
)
8889

8990
projectOptions, err := composecli.NewProjectOptions(o.ConfigPaths, optionsFn...)
@@ -95,19 +96,6 @@ func New(o Options, client *containerd.Client) (*Composer, error) {
9596
return nil, err
9697
}
9798

98-
if len(o.Services) > 0 {
99-
s, err := project.GetServices(o.Services...)
100-
if err != nil {
101-
return nil, err
102-
}
103-
o.Profiles = append(o.Profiles, s.GetProfiles()...)
104-
}
105-
106-
project, err = project.WithProfiles(o.Profiles)
107-
if err != nil {
108-
return nil, err
109-
}
110-
11199
if o.DebugPrintFull {
112100
projectJSON, _ := json.MarshalIndent(project, "", " ")
113101
log.L.Debug("printing project JSON")

0 commit comments

Comments
 (0)