diff --git a/pkg/compose/convergence.go b/pkg/compose/convergence.go index 1f9dac1c0..f65247b08 100644 --- a/pkg/compose/convergence.go +++ b/pkg/compose/convergence.go @@ -134,14 +134,7 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project, container := container traceOpts := append(tracing.ServiceOptions(service), tracing.ContainerOptions(container)...) eg.Go(tracing.SpanWrapFuncForErrGroup(ctx, "service/scale/down", traceOpts, func(ctx context.Context) error { - timeoutInSecond := utils.DurationSecondToInt(timeout) - err := c.service.apiClient().ContainerStop(ctx, container.ID, containerType.StopOptions{ - Timeout: timeoutInSecond, - }) - if err != nil { - return err - } - return c.service.apiClient().ContainerRemove(ctx, container.ID, containerType.RemoveOptions{}) + return c.service.stopAndRemoveContainer(ctx, container, timeout, false) })) continue } diff --git a/pkg/compose/create.go b/pkg/compose/create.go index 1ac5a1167..1bf9289fe 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -99,8 +99,7 @@ func (s *composeService) create(ctx context.Context, project *types.Project, opt orphans := observedState.filter(isNotService(allServiceNames...)) if len(orphans) > 0 && !options.IgnoreOrphans { if options.RemoveOrphans { - w := progress.ContextWriter(ctx) - err := s.removeContainers(ctx, w, orphans, nil, false) + err := s.removeContainers(ctx, orphans, nil, false) if err != nil { return err } diff --git a/pkg/compose/down.go b/pkg/compose/down.go index 009dcf8f5..7c344b078 100644 --- a/pkg/compose/down.go +++ b/pkg/compose/down.go @@ -76,7 +76,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a err = InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error { serviceContainers := containers.filter(isService(service)) - err := s.removeContainers(ctx, w, serviceContainers, options.Timeout, options.Volumes) + err := s.removeContainers(ctx, serviceContainers, options.Timeout, options.Volumes) return err }, WithRootNodesAndDown(options.Services)) if err != nil { @@ -85,7 +85,7 @@ func (s *composeService) down(ctx context.Context, projectName string, options a orphans := containers.filter(isOrphaned(project)) if options.RemoveOrphans && len(orphans) > 0 { - err := s.removeContainers(ctx, w, orphans, options.Timeout, false) + err := s.removeContainers(ctx, orphans, options.Timeout, false) if err != nil { return err } @@ -303,32 +303,37 @@ func (s *composeService) stopContainers(ctx context.Context, w progress.Writer, return eg.Wait() } -func (s *composeService) removeContainers(ctx context.Context, w progress.Writer, containers []moby.Container, timeout *time.Duration, volumes bool) error { +func (s *composeService) removeContainers(ctx context.Context, containers []moby.Container, timeout *time.Duration, volumes bool) error { eg, _ := errgroup.WithContext(ctx) for _, container := range containers { container := container eg.Go(func() error { - eventName := getContainerProgressName(container) - err := s.stopContainer(ctx, w, container, timeout) - if err != nil { - return err - } - w.Event(progress.RemovingEvent(eventName)) - err = s.apiClient().ContainerRemove(ctx, container.ID, containerType.RemoveOptions{ - Force: true, - RemoveVolumes: volumes, - }) - if err != nil && !errdefs.IsNotFound(err) && !errdefs.IsConflict(err) { - w.Event(progress.ErrorMessageEvent(eventName, "Error while Removing")) - return err - } - w.Event(progress.RemovedEvent(eventName)) - return nil + return s.stopAndRemoveContainer(ctx, container, timeout, volumes) }) } return eg.Wait() } +func (s *composeService) stopAndRemoveContainer(ctx context.Context, container moby.Container, timeout *time.Duration, volumes bool) error { + w := progress.ContextWriter(ctx) + eventName := getContainerProgressName(container) + err := s.stopContainer(ctx, w, container, timeout) + if err != nil { + return err + } + w.Event(progress.RemovingEvent(eventName)) + err = s.apiClient().ContainerRemove(ctx, container.ID, containerType.RemoveOptions{ + Force: true, + RemoveVolumes: volumes, + }) + if err != nil && !errdefs.IsNotFound(err) && !errdefs.IsConflict(err) { + w.Event(progress.ErrorMessageEvent(eventName, "Error while Removing")) + return err + } + w.Event(progress.RemovedEvent(eventName)) + return nil +} + func (s *composeService) getProjectWithResources(ctx context.Context, containers Containers, projectName string) (*types.Project, error) { containers = containers.filter(isNotOneOff) project, err := s.projectFromName(containers, projectName)