remove.go 3.0 KB

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