diff --git a/cli/cmd/compose/run.go b/cli/cmd/compose/run.go index 27cab00f0..1fd6ef74c 100644 --- a/cli/cmd/compose/run.go +++ b/cli/cmd/compose/run.go @@ -69,15 +69,13 @@ func runRun(ctx context.Context, opts runOptions) error { return err } - originalServices := project.Services _, err = progress.Run(ctx, func(ctx context.Context) (string, error) { - return "", startDependencies(ctx, c, project, opts.Service) + return "", startDependencies(ctx, c, *project, opts.Service) }) if err != nil { return err } - project.Services = originalServices // start container and attach to container streams runOpts := compose.RunOptions{ Service: opts.Service, @@ -90,21 +88,24 @@ func runRun(ctx context.Context, opts runOptions) error { return c.ComposeService().RunOneOffContainer(ctx, project, runOpts) } -func startDependencies(ctx context.Context, c *client.Client, project *types.Project, requestedService string) error { - originalServices := project.Services +func startDependencies(ctx context.Context, c *client.Client, project types.Project, requestedServiceName string) error { dependencies := types.Services{} - for _, service := range originalServices { - if service.Name != requestedService { + var requestedService types.ServiceConfig + for _, service := range project.Services { + if service.Name != requestedServiceName { dependencies = append(dependencies, service) + } else { + requestedService = service } } + project.Services = dependencies - if err := c.ComposeService().Create(ctx, project, compose.CreateOptions{}); err != nil { + project.DisabledServices = append(project.DisabledServices, requestedService) + if err := c.ComposeService().Create(ctx, &project, compose.CreateOptions{}); err != nil { return err } - if err := c.ComposeService().Start(ctx, project, compose.StartOptions{}); err != nil { + if err := c.ComposeService().Start(ctx, &project, compose.StartOptions{}); err != nil { return err } return nil - } diff --git a/local/compose/create.go b/local/compose/create.go index 7e4f2af8d..f436f8d29 100644 --- a/local/compose/create.go +++ b/local/compose/create.go @@ -73,7 +73,12 @@ func (s *composeService) Create(ctx context.Context, project *types.Project, opt return err } - orphans := observedState.filter(isNotService(project.ServiceNames()...)) + allServices := project.AllServices() + allServiceNames := []string{} + for _, service := range allServices { + allServiceNames = append(allServiceNames, service.Name) + } + orphans := observedState.filter(isNotService(allServiceNames...)) if len(orphans) > 0 { if opts.RemoveOrphans { w := progress.ContextWriter(ctx) diff --git a/local/e2e/compose/compose_test.go b/local/e2e/compose/compose_test.go index b61234371..1d449333e 100644 --- a/local/e2e/compose/compose_test.go +++ b/local/e2e/compose/compose_test.go @@ -122,6 +122,12 @@ func TestLocalComposeRun(t *testing.T) { res := c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yml", "run", "back") lines := Lines(res.Stdout()) assert.Equal(t, lines[len(lines)-1], "Hello there!!", res.Stdout()) + assert.Assert(t, !strings.Contains(res.Combined(), "orphan")) + + res = c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yml", "run", "back", "echo", "Hello one more time") + lines = Lines(res.Stdout()) + assert.Equal(t, lines[len(lines)-1], "Hello one more time", res.Stdout()) + assert.Assert(t, !strings.Contains(res.Combined(), "orphan")) }) t.Run("check run container exited", func(t *testing.T) { @@ -156,10 +162,8 @@ func TestLocalComposeRun(t *testing.T) { res := c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yml", "run", "--rm", "back", "/bin/sh", "-c", "echo Hello again") lines := Lines(res.Stdout()) assert.Equal(t, lines[len(lines)-1], "Hello again", res.Stdout()) - }) - t.Run("check run container removed", func(t *testing.T) { - res := c.RunDockerCmd("ps", "--all") + res = c.RunDockerCmd("ps", "--all") assert.Assert(t, strings.Contains(res.Stdout(), "run-test_back"), res.Stdout()) })