Skip to content

Commit 13e5e80

Browse files
committed
fix run --build support for service:* reference in additional_context
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 6078b4d commit 13e5e80

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

pkg/compose/build.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,19 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
7979
policy = types.IncludeDependencies
8080
}
8181

82+
if len(options.Services) == 0 {
83+
options.Services = project.ServiceNames()
84+
}
85+
86+
// also include services used as additional_contexts ith service: prefix
87+
options.Services = addBuildDependencies(options.Services, project)
88+
// Some build dependencies we just introduced may not be enabled
8289
var err error
83-
if len(options.Services) > 0 {
84-
// As user requested some services to be built, also include those used as additional_contexts
85-
options.Services = addBuildDependencies(options.Services, project)
86-
// Some build dependencies we just introduced may not be enabled
87-
project, err = project.WithServicesEnabled(options.Services...)
88-
if err != nil {
89-
return nil, err
90-
}
90+
project, err = project.WithServicesEnabled(options.Services...)
91+
if err != nil {
92+
return nil, err
9193
}
94+
9295
project, err = project.WithSelectedServices(options.Services)
9396
if err != nil {
9497
return nil, err

pkg/e2e/build_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
)
3333

3434
func TestLocalComposeBuild(t *testing.T) {
35-
for _, env := range []string{"DOCKER_BUILDKIT=0", "DOCKER_BUILDKIT=1", "DOCKER_BUILDKIT=1,COMPOSE-BAKE=1"} {
35+
for _, env := range []string{"DOCKER_BUILDKIT=0", "DOCKER_BUILDKIT=1,COMPOSE-BAKE=0", "DOCKER_BUILDKIT=1,COMPOSE-BAKE=1"} {
3636
c := NewCLI(t, WithEnv(strings.Split(env, ",")...))
3737

3838
t.Run(env+" build named and unnamed images", func(t *testing.T) {

pkg/e2e/compose_run_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,10 @@ func TestLocalComposeRun(t *testing.T) {
216216
"front", "env")
217217
res.Assert(t, icmd.Expected{Out: "FOO=BAR"})
218218
})
219+
220+
t.Run("compose run --build", func(t *testing.T) {
221+
c.cleanupWithDown(t, "run-test", "--rmi=local")
222+
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "build", "echo", "hello world")
223+
res.Assert(t, icmd.Expected{Out: "hello world"})
224+
})
219225
}

pkg/e2e/fixtures/run-test/compose.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ services:
1616
image: nginx:alpine
1717
networks:
1818
- frontnet
19+
build:
20+
build:
21+
dockerfile_inline: "FROM base"
22+
additional_contexts:
23+
base: "service:build_base"
24+
build_base:
25+
build:
26+
dockerfile_inline: "FROM alpine"
1927
networks:
2028
frontnet:
2129
backnet:

0 commit comments

Comments
 (0)