stop.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. moby "github.com/docker/docker/api/types"
  17. "github.com/docker/docker/api/types/filters"
  18. "github.com/docker/compose-cli/api/compose"
  19. "github.com/docker/compose-cli/api/progress"
  20. "github.com/compose-spec/compose-go/types"
  21. "golang.org/x/sync/errgroup"
  22. )
  23. func (s *composeService) Stop(ctx context.Context, project *types.Project, consumer compose.LogConsumer) error {
  24. eg, _ := errgroup.WithContext(ctx)
  25. w := progress.ContextWriter(ctx)
  26. var containers Containers
  27. containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
  28. Filters: filters.NewArgs(projectFilter(project.Name)),
  29. All: true,
  30. })
  31. if err != nil {
  32. return err
  33. }
  34. err = InReverseDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
  35. serviceContainers, others := containers.split(isService(service.Name))
  36. err := s.stopContainers(ctx, w, serviceContainers)
  37. containers = others
  38. return err
  39. })
  40. if err != nil {
  41. return err
  42. }
  43. return eg.Wait()
  44. }