remove.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. containerType "github.com/docker/docker/api/types/container"
  21. "golang.org/x/sync/errgroup"
  22. "github.com/docker/compose/v2/pkg/progress"
  23. "github.com/docker/compose/v2/pkg/prompt"
  24. )
  25. func (s *composeService) Remove(ctx context.Context, projectName string, options api.RemoveOptions) error {
  26. projectName = strings.ToLower(projectName)
  27. if options.Stop {
  28. err := s.Stop(ctx, projectName, api.StopOptions{
  29. Services: options.Services,
  30. Project: options.Project,
  31. })
  32. if err != nil {
  33. return err
  34. }
  35. }
  36. containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...)
  37. if err != nil {
  38. if api.IsNotFoundError(err) {
  39. _, _ = fmt.Fprintln(s.stderr(), "No stopped containers")
  40. return nil
  41. }
  42. return err
  43. }
  44. if options.Project != nil {
  45. containers = containers.filter(isService(options.Project.ServiceNames()...))
  46. }
  47. var stoppedContainers Containers
  48. for _, container := range containers {
  49. // We have to inspect containers, as State reported by getContainers suffers a race condition
  50. inspected, err := s.apiClient().ContainerInspect(ctx, container.ID)
  51. if api.IsNotFoundError(err) {
  52. // Already removed. Maybe configured with auto-remove
  53. continue
  54. }
  55. if err != nil {
  56. return err
  57. }
  58. if !inspected.State.Running || (options.Stop && s.dryRun) {
  59. stoppedContainers = append(stoppedContainers, container)
  60. }
  61. }
  62. var names []string
  63. stoppedContainers.forEach(func(c moby.Container) {
  64. names = append(names, getCanonicalContainerName(c))
  65. })
  66. if len(names) == 0 {
  67. _, _ = fmt.Fprintln(s.stdinfo(), "No stopped containers")
  68. return nil
  69. }
  70. msg := fmt.Sprintf("Going to remove %s", strings.Join(names, ", "))
  71. if options.Force {
  72. _, _ = fmt.Fprintln(s.stdout(), msg)
  73. } else {
  74. confirm, err := prompt.NewPrompt(s.stdin(), s.stdout()).Confirm(msg, false)
  75. if err != nil {
  76. return err
  77. }
  78. if !confirm {
  79. return nil
  80. }
  81. }
  82. return progress.RunWithTitle(ctx, func(ctx context.Context) error {
  83. return s.remove(ctx, stoppedContainers, options)
  84. }, s.stdinfo(), "Removing")
  85. }
  86. func (s *composeService) remove(ctx context.Context, containers Containers, options api.RemoveOptions) error {
  87. w := progress.ContextWriter(ctx)
  88. eg, ctx := errgroup.WithContext(ctx)
  89. for _, container := range containers {
  90. container := container
  91. eg.Go(func() error {
  92. eventName := getContainerProgressName(container)
  93. w.Event(progress.RemovingEvent(eventName))
  94. err := s.apiClient().ContainerRemove(ctx, container.ID, containerType.RemoveOptions{
  95. RemoveVolumes: options.Volumes,
  96. Force: options.Force,
  97. })
  98. if err == nil {
  99. w.Event(progress.RemovedEvent(eventName))
  100. }
  101. return err
  102. })
  103. }
  104. return eg.Wait()
  105. }