start.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. "github.com/docker/compose-cli/api/compose"
  17. "github.com/compose-spec/compose-go/types"
  18. "github.com/docker/docker/api/types/container"
  19. "github.com/sirupsen/logrus"
  20. )
  21. func (s *composeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
  22. var containers Containers
  23. if options.Attach != nil {
  24. c, err := s.attach(ctx, project, options.Attach)
  25. if err != nil {
  26. return err
  27. }
  28. containers = c
  29. }
  30. err := InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
  31. return s.startService(ctx, project, service)
  32. })
  33. if err != nil {
  34. return err
  35. }
  36. if options.Attach == nil {
  37. return nil
  38. }
  39. for _, c := range containers {
  40. c := c
  41. go func() {
  42. statusC, errC := s.apiClient.ContainerWait(context.Background(), c.ID, container.WaitConditionNotRunning)
  43. select {
  44. case status := <-statusC:
  45. options.Attach(compose.ContainerEvent{
  46. Type: compose.ContainerEventExit,
  47. Source: c.ID,
  48. Name: getCanonicalContainerName(c),
  49. Service: c.Labels[serviceLabel],
  50. ExitCode: int(status.StatusCode),
  51. })
  52. case err := <-errC:
  53. logrus.Warnf("Unexpected API error for %s : %s\n", getCanonicalContainerName(c), err.Error())
  54. }
  55. }()
  56. }
  57. return nil
  58. }