Skip to content

Commit 449a46a

Browse files
authored
Merge pull request docker#10423 from ndeloof/build_classic_panic
prevent panic using classic builder
2 parents 02ad467 + 981cb20 commit 449a46a

File tree

2 files changed

+81
-73
lines changed

2 files changed

+81
-73
lines changed

pkg/compose/build.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
7575
}
7676

7777
if !buildkitEnabled {
78-
service.Build.Args = service.Build.Args.OverrideBy(args)
78+
if service.Build.Args == nil {
79+
service.Build.Args = args
80+
} else {
81+
service.Build.Args = service.Build.Args.OverrideBy(args)
82+
}
7983
id, err := s.doBuildClassic(ctx, service)
8084
if err != nil {
8185
return err

pkg/e2e/build_test.go

Lines changed: 76 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -29,100 +29,104 @@ import (
2929
)
3030

3131
func TestLocalComposeBuild(t *testing.T) {
32-
c := NewParallelCLI(t)
3332

34-
t.Run("build named and unnamed images", func(t *testing.T) {
35-
// ensure local test run does not reuse previously build image
36-
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
37-
c.RunDockerOrExitError(t, "rmi", "custom-nginx")
33+
for _, env := range []string{"DOCKER_BUILDKIT=0", "DOCKER_BUILDKIT=1"} {
34+
c := NewCLI(t, WithEnv(env))
3835

39-
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "build")
36+
t.Run(env+" build named and unnamed images", func(t *testing.T) {
37+
// ensure local test run does not reuse previously build image
38+
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
39+
c.RunDockerOrExitError(t, "rmi", "custom-nginx")
4040

41-
res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
42-
c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
43-
c.RunDockerCmd(t, "image", "inspect", "custom-nginx")
44-
})
41+
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "build")
4542

46-
t.Run("build with build-arg", func(t *testing.T) {
47-
// ensure local test run does not reuse previously build image
48-
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
49-
c.RunDockerOrExitError(t, "rmi", "custom-nginx")
43+
res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
44+
c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
45+
c.RunDockerCmd(t, "image", "inspect", "custom-nginx")
46+
})
5047

51-
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "build", "--build-arg", "FOO=BAR")
48+
t.Run(env+" build with build-arg", func(t *testing.T) {
49+
// ensure local test run does not reuse previously build image
50+
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
51+
c.RunDockerOrExitError(t, "rmi", "custom-nginx")
5252

53-
res := c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
54-
res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
55-
})
53+
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "build", "--build-arg", "FOO=BAR")
5654

57-
t.Run("build with build-arg set by env", func(t *testing.T) {
58-
// ensure local test run does not reuse previously build image
59-
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
60-
c.RunDockerOrExitError(t, "rmi", "custom-nginx")
61-
62-
icmd.RunCmd(c.NewDockerComposeCmd(t,
63-
"--project-directory",
64-
"fixtures/build-test",
65-
"build",
66-
"--build-arg",
67-
"FOO"),
68-
func(cmd *icmd.Cmd) {
69-
cmd.Env = append(cmd.Env, "FOO=BAR")
70-
})
55+
res := c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
56+
res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
57+
})
7158

72-
res := c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
73-
res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
74-
})
59+
t.Run(env+" build with build-arg set by env", func(t *testing.T) {
60+
// ensure local test run does not reuse previously build image
61+
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
62+
c.RunDockerOrExitError(t, "rmi", "custom-nginx")
63+
64+
icmd.RunCmd(c.NewDockerComposeCmd(t,
65+
"--project-directory",
66+
"fixtures/build-test",
67+
"build",
68+
"--build-arg",
69+
"FOO"),
70+
func(cmd *icmd.Cmd) {
71+
cmd.Env = append(cmd.Env, "FOO=BAR")
72+
})
73+
74+
res := c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
75+
res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
76+
})
7577

76-
t.Run("build with multiple build-args ", func(t *testing.T) {
77-
// ensure local test run does not reuse previously build image
78-
c.RunDockerOrExitError(t, "rmi", "-f", "multi-args-multiargs")
79-
cmd := c.NewDockerComposeCmd(t, "--project-directory", "fixtures/build-test/multi-args", "build")
78+
t.Run(env+" build with multiple build-args ", func(t *testing.T) {
79+
// ensure local test run does not reuse previously build image
80+
c.RunDockerOrExitError(t, "rmi", "-f", "multi-args-multiargs")
81+
cmd := c.NewDockerComposeCmd(t, "--project-directory", "fixtures/build-test/multi-args", "build")
8082

81-
icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
82-
cmd.Env = append(cmd.Env, "DOCKER_BUILDKIT=0")
83+
icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
84+
cmd.Env = append(cmd.Env, "DOCKER_BUILDKIT=0")
85+
})
86+
87+
res := c.RunDockerCmd(t, "image", "inspect", "multi-args-multiargs")
88+
res.Assert(t, icmd.Expected{Out: `"RESULT": "SUCCESS"`})
8389
})
8490

85-
res := c.RunDockerCmd(t, "image", "inspect", "multi-args-multiargs")
86-
res.Assert(t, icmd.Expected{Out: `"RESULT": "SUCCESS"`})
87-
})
91+
t.Run(env+" build as part of up", func(t *testing.T) {
92+
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
93+
c.RunDockerOrExitError(t, "rmi", "custom-nginx")
8894

89-
t.Run("build as part of up", func(t *testing.T) {
90-
c.RunDockerOrExitError(t, "rmi", "build-test-nginx")
91-
c.RunDockerOrExitError(t, "rmi", "custom-nginx")
95+
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "up", "-d")
96+
t.Cleanup(func() {
97+
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "down")
98+
})
9299

93-
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "up", "-d")
94-
t.Cleanup(func() {
95-
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "down")
96-
})
100+
res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
101+
res.Assert(t, icmd.Expected{Out: "COPY static2 /usr/share/nginx/html"})
97102

98-
res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
99-
res.Assert(t, icmd.Expected{Out: "COPY static2 /usr/share/nginx/html"})
103+
output := HTTPGetWithRetry(t, "http://localhost:8070", http.StatusOK, 2*time.Second, 20*time.Second)
104+
assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
100105

101-
output := HTTPGetWithRetry(t, "http://localhost:8070", http.StatusOK, 2*time.Second, 20*time.Second)
102-
assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
106+
c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
107+
c.RunDockerCmd(t, "image", "inspect", "custom-nginx")
108+
})
103109

104-
c.RunDockerCmd(t, "image", "inspect", "build-test-nginx")
105-
c.RunDockerCmd(t, "image", "inspect", "custom-nginx")
106-
})
110+
t.Run(env+" no rebuild when up again", func(t *testing.T) {
111+
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "up", "-d")
107112

108-
t.Run("no rebuild when up again", func(t *testing.T) {
109-
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "up", "-d")
113+
assert.Assert(t, !strings.Contains(res.Stdout(), "COPY static"), res.Stdout())
114+
})
110115

111-
assert.Assert(t, !strings.Contains(res.Stdout(), "COPY static"), res.Stdout())
112-
})
116+
t.Run(env+" rebuild when up --build", func(t *testing.T) {
117+
res := c.RunDockerComposeCmd(t, "--workdir", "fixtures/build-test", "up", "-d", "--build")
113118

114-
t.Run("rebuild when up --build", func(t *testing.T) {
115-
res := c.RunDockerComposeCmd(t, "--workdir", "fixtures/build-test", "up", "-d", "--build")
119+
res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
120+
res.Assert(t, icmd.Expected{Out: "COPY static2 /usr/share/nginx/html"})
121+
})
116122

117-
res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
118-
res.Assert(t, icmd.Expected{Out: "COPY static2 /usr/share/nginx/html"})
119-
})
123+
t.Run(env+" cleanup build project", func(t *testing.T) {
124+
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "down")
125+
c.RunDockerCmd(t, "rmi", "build-test-nginx")
126+
c.RunDockerCmd(t, "rmi", "custom-nginx")
127+
})
128+
}
120129

121-
t.Run("cleanup build project", func(t *testing.T) {
122-
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "down")
123-
c.RunDockerCmd(t, "rmi", "build-test-nginx")
124-
c.RunDockerCmd(t, "rmi", "custom-nginx")
125-
})
126130
}
127131

128132
func TestBuildSSH(t *testing.T) {

0 commit comments

Comments
 (0)