1
0

remove.go 3.1 KB

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