Browse Source

Only start direct dependencies of service on `compose run ...`

Signed-off-by: Laura Brehm <[email protected]>
Laura Brehm 3 years ago
parent
commit
80b7a8d274
3 changed files with 29 additions and 1 deletions
  1. 3 1
      cmd/compose/run.go
  2. 12 0
      pkg/e2e/compose_run_test.go
  3. 14 0
      pkg/e2e/fixtures/run-test/deps.yaml

+ 3 - 1
cmd/compose/run.go

@@ -262,7 +262,9 @@ func startDependencies(ctx context.Context, backend api.Service, project types.P
 	}
 
 	if len(dependencies) > 0 {
-		return backend.Start(ctx, project.Name, api.StartOptions{})
+		return backend.Start(ctx, project.Name, api.StartOptions{
+			Project: &project,
+		})
 	}
 	return nil
 }

+ 12 - 0
pkg/e2e/compose_run_test.go

@@ -117,4 +117,16 @@ func TestLocalComposeRun(t *testing.T) {
 		res := c.RunDockerCmd("ps", "--all")
 		assert.Assert(t, !strings.Contains(res.Stdout(), "run-test"), res.Stdout())
 	})
+
+	t.Run("run starts only container and dependencies", func(t *testing.T) {
+		// ensure that even if another service is up run does not start it: https://github.com/docker/compose/issues/9459
+		res := c.RunDockerComposeCmd("-f", "./fixtures/run-test/deps.yaml", "up", "service_b")
+		res.Assert(t, icmd.Success)
+
+		res = c.RunDockerComposeCmd("-f", "./fixtures/run-test/deps.yaml", "run", "service_a")
+		assert.Assert(t, strings.Contains(res.Combined(), "shared_dep"), res.Combined())
+		assert.Assert(t, !strings.Contains(res.Combined(), "service_b"), res.Combined())
+
+		c.RunDockerComposeCmd("-f", "./fixtures/run-test/deps.yaml", "down", "--remove-orphans")
+	})
 }

+ 14 - 0
pkg/e2e/fixtures/run-test/deps.yaml

@@ -0,0 +1,14 @@
+version: "3.6"
+services:
+  service_a:
+    image: bash
+    command: echo "a"
+    depends_on:
+      - shared_dep
+  service_b:
+    image: bash
+    command: echo "b"
+    depends_on:
+      - shared_dep
+  shared_dep:
+    image: bash