remove.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "fmt"
  17. "strings"
  18. "github.com/docker/compose/v2/pkg/api"
  19. moby "github.com/docker/docker/api/types"
  20. "golang.org/x/sync/errgroup"
  21. "github.com/docker/compose/v2/pkg/progress"
  22. "github.com/docker/compose/v2/pkg/prompt"
  23. )
  24. func (s *composeService) Remove(ctx context.Context, projectName string, options api.RemoveOptions) error {
  25. containers, _, err := s.actualState(ctx, projectName, options.Services)
  26. if err != nil {
  27. if api.IsNotFoundError(err) {
  28. fmt.Fprintln(s.stderr(), "No stopped containers")
  29. return nil
  30. }
  31. return err
  32. }
  33. stoppedContainers := containers.filter(func(c moby.Container) bool {
  34. return c.State != ContainerRunning
  35. })
  36. var names []string
  37. stoppedContainers.forEach(func(c moby.Container) {
  38. names = append(names, getCanonicalContainerName(c))
  39. })
  40. if len(names) == 0 {
  41. fmt.Fprintln(s.stderr(), "No stopped containers")
  42. return nil
  43. }
  44. msg := fmt.Sprintf("Going to remove %s", strings.Join(names, ", "))
  45. if options.Force {
  46. fmt.Println(msg)
  47. } else {
  48. confirm, err := prompt.User{}.Confirm(msg, false)
  49. if err != nil {
  50. return err
  51. }
  52. if !confirm {
  53. return nil
  54. }
  55. }
  56. return progress.Run(ctx, func(ctx context.Context) error {
  57. return s.remove(ctx, stoppedContainers, options)
  58. })
  59. }
  60. func (s *composeService) remove(ctx context.Context, containers Containers, options api.RemoveOptions) error {
  61. w := progress.ContextWriter(ctx)
  62. eg, ctx := errgroup.WithContext(ctx)
  63. for _, container := range containers {
  64. container := container
  65. eg.Go(func() error {
  66. eventName := getContainerProgressName(container)
  67. w.Event(progress.RemovingEvent(eventName))
  68. err := s.apiClient().ContainerRemove(ctx, container.ID, moby.ContainerRemoveOptions{
  69. RemoveVolumes: options.Volumes,
  70. Force: options.Force,
  71. })
  72. if err == nil {
  73. w.Event(progress.RemovedEvent(eventName))
  74. }
  75. return err
  76. })
  77. }
  78. return eg.Wait()
  79. }