remove.go 3.3 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. "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. if options.Stop {
  27. err := s.Stop(ctx, projectName, api.StopOptions{
  28. Services: options.Services,
  29. Project: options.Project,
  30. })
  31. if err != nil {
  32. return err
  33. }
  34. }
  35. containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, options.Services...)
  36. if err != nil {
  37. if api.IsNotFoundError(err) {
  38. fmt.Fprintln(s.stderr(), "No stopped containers")
  39. return nil
  40. }
  41. return err
  42. }
  43. if options.Project != nil {
  44. containers = containers.filter(isService(options.Project.ServiceNames()...))
  45. }
  46. var stoppedContainers Containers
  47. for _, container := range containers {
  48. // We have to inspect containers, as State reported by getContainers suffers a race condition
  49. inspected, err := s.apiClient().ContainerInspect(ctx, container.ID)
  50. if api.IsNotFoundError(err) {
  51. // Already removed. Maybe configured with auto-remove
  52. continue
  53. }
  54. if err != nil {
  55. return err
  56. }
  57. if !inspected.State.Running || (options.Stop && s.dryRun) {
  58. stoppedContainers = append(stoppedContainers, container)
  59. }
  60. }
  61. var names []string
  62. stoppedContainers.forEach(func(c moby.Container) {
  63. names = append(names, getCanonicalContainerName(c))
  64. })
  65. fmt.Fprintln(s.stdinfo(), names)
  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, moby.ContainerRemoveOptions{
  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. }