remove.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. projectName = strings.ToLower(projectName)
  26. containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...)
  27. if err != nil {
  28. if api.IsNotFoundError(err) {
  29. fmt.Fprintln(s.stderr(), "No stopped containers")
  30. return nil
  31. }
  32. return err
  33. }
  34. if options.Project != nil {
  35. containers = containers.filter(isService(options.Project.ServiceNames()...))
  36. }
  37. stoppedContainers := containers.filter(func(c moby.Container) bool {
  38. return c.State != ContainerRunning
  39. })
  40. var names []string
  41. stoppedContainers.forEach(func(c moby.Container) {
  42. names = append(names, getCanonicalContainerName(c))
  43. })
  44. if len(names) == 0 {
  45. fmt.Fprintln(s.stderr(), "No stopped containers")
  46. return nil
  47. }
  48. msg := fmt.Sprintf("Going to remove %s", strings.Join(names, ", "))
  49. if options.Force {
  50. fmt.Fprintln(s.stdout(), msg)
  51. } else {
  52. confirm, err := prompt.User{}.Confirm(msg, false)
  53. if err != nil {
  54. return err
  55. }
  56. if !confirm {
  57. return nil
  58. }
  59. }
  60. return progress.Run(ctx, func(ctx context.Context) error {
  61. return s.remove(ctx, stoppedContainers, options)
  62. })
  63. }
  64. func (s *composeService) remove(ctx context.Context, containers Containers, options api.RemoveOptions) error {
  65. w := progress.ContextWriter(ctx)
  66. eg, ctx := errgroup.WithContext(ctx)
  67. for _, container := range containers {
  68. container := container
  69. eg.Go(func() error {
  70. eventName := getContainerProgressName(container)
  71. w.Event(progress.RemovingEvent(eventName))
  72. err := s.apiClient().ContainerRemove(ctx, container.ID, moby.ContainerRemoveOptions{
  73. RemoveVolumes: options.Volumes,
  74. Force: options.Force,
  75. })
  76. if err == nil {
  77. w.Event(progress.RemovedEvent(eventName))
  78. }
  79. return err
  80. })
  81. }
  82. return eg.Wait()
  83. }