Browse Source

fix run --build support for service:* reference in additional_context

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 3 months ago
parent
commit
09689400e5

+ 11 - 8
pkg/compose/build.go

@@ -79,16 +79,19 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
 		policy = types.IncludeDependencies
 	}
 
+	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
-	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
-		}
+	project, err = project.WithServicesEnabled(options.Services...)
+	if err != nil {
+		return nil, err
 	}
+
 	project, err = project.WithSelectedServices(options.Services)
 	if err != nil {
 		return nil, err

+ 1 - 1
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) {

+ 6 - 0
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"})
+	})
 }

+ 8 - 0
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: