From 09689400e53f5bba13b52ae8f526590896789bd2 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 27 Aug 2025 15:51:40 +0200 Subject: [PATCH] fix run --build support for service:* reference in additional_context Signed-off-by: Nicolas De Loof --- pkg/compose/build.go | 21 ++++++++++++--------- pkg/e2e/build_test.go | 2 +- pkg/e2e/compose_run_test.go | 6 ++++++ pkg/e2e/fixtures/run-test/compose.yaml | 8 ++++++++ 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/pkg/compose/build.go b/pkg/compose/build.go index c2fadee41..4fa3fa309 100644 --- a/pkg/compose/build.go +++ b/pkg/compose/build.go @@ -79,16 +79,19 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti policy = types.IncludeDependencies } - var err error - if len(options.Services) > 0 { - // As user requested some services to be built, also include those used as additional_contexts - options.Services = addBuildDependencies(options.Services, project) - // Some build dependencies we just introduced may not be enabled - project, err = project.WithServicesEnabled(options.Services...) - if err != nil { - return nil, err - } + if len(options.Services) == 0 { + options.Services = project.ServiceNames() } + + // also include services used as additional_contexts with service: prefix + options.Services = addBuildDependencies(options.Services, project) + // Some build dependencies we just introduced may not be enabled + var err error + project, err = project.WithServicesEnabled(options.Services...) + if err != nil { + return nil, err + } + project, err = project.WithSelectedServices(options.Services) if err != nil { return nil, err diff --git a/pkg/e2e/build_test.go b/pkg/e2e/build_test.go index 202e2d88d..8f37e7e52 100644 --- a/pkg/e2e/build_test.go +++ b/pkg/e2e/build_test.go @@ -32,7 +32,7 @@ import ( ) func TestLocalComposeBuild(t *testing.T) { - for _, env := range []string{"DOCKER_BUILDKIT=0", "DOCKER_BUILDKIT=1", "DOCKER_BUILDKIT=1,COMPOSE-BAKE=1"} { + for _, env := range []string{"DOCKER_BUILDKIT=0", "DOCKER_BUILDKIT=1,COMPOSE_BAKE=0", "DOCKER_BUILDKIT=1,COMPOSE_BAKE=1"} { c := NewCLI(t, WithEnv(strings.Split(env, ",")...)) t.Run(env+" build named and unnamed images", func(t *testing.T) { diff --git a/pkg/e2e/compose_run_test.go b/pkg/e2e/compose_run_test.go index abef1a539..0878c9731 100644 --- a/pkg/e2e/compose_run_test.go +++ b/pkg/e2e/compose_run_test.go @@ -216,4 +216,10 @@ func TestLocalComposeRun(t *testing.T) { "front", "env") res.Assert(t, icmd.Expected{Out: "FOO=BAR"}) }) + + t.Run("compose run --build", func(t *testing.T) { + c.cleanupWithDown(t, "run-test", "--rmi=local") + res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "build", "echo", "hello world") + res.Assert(t, icmd.Expected{Out: "hello world"}) + }) } diff --git a/pkg/e2e/fixtures/run-test/compose.yaml b/pkg/e2e/fixtures/run-test/compose.yaml index 8162dba08..aef801190 100644 --- a/pkg/e2e/fixtures/run-test/compose.yaml +++ b/pkg/e2e/fixtures/run-test/compose.yaml @@ -16,6 +16,14 @@ services: image: nginx:alpine networks: - frontnet + build: + build: + dockerfile_inline: "FROM base" + additional_contexts: + base: "service:build_base" + build_base: + build: + dockerfile_inline: "FROM alpine" networks: frontnet: backnet: