start.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/compose-spec/compose-go/types"
  17. moby "github.com/docker/docker/api/types"
  18. "github.com/pkg/errors"
  19. "golang.org/x/sync/errgroup"
  20. "github.com/docker/compose-cli/api/compose"
  21. "github.com/docker/compose-cli/api/progress"
  22. )
  23. func (s *composeService) Start(ctx context.Context, project *types.Project, options compose.StartOptions) error {
  24. return progress.Run(ctx, func(ctx context.Context) error {
  25. return s.start(ctx, project, options, nil)
  26. })
  27. }
  28. func (s *composeService) start(ctx context.Context, project *types.Project, options compose.StartOptions, listener func(event compose.ContainerEvent)) error {
  29. if len(options.AttachTo) == 0 {
  30. options.AttachTo = project.ServiceNames()
  31. }
  32. eg, ctx := errgroup.WithContext(ctx)
  33. if listener != nil {
  34. attached, err := s.attach(ctx, project, listener, options.AttachTo)
  35. if err != nil {
  36. return err
  37. }
  38. eg.Go(func() error {
  39. return s.watchContainers(project, options.AttachTo, listener, attached)
  40. })
  41. }
  42. err := InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
  43. return s.startService(ctx, project, service)
  44. })
  45. if err != nil {
  46. return err
  47. }
  48. return eg.Wait()
  49. }
  50. // watchContainers uses engine events to capture container start/die and notify ContainerEventListener
  51. func (s *composeService) watchContainers(project *types.Project, services []string, listener compose.ContainerEventListener, containers Containers) error {
  52. watched := map[string]int{}
  53. for _, c := range containers {
  54. watched[c.ID] = 0
  55. }
  56. ctx, stop := context.WithCancel(context.Background())
  57. err := s.Events(ctx, project.Name, compose.EventsOptions{
  58. Services: services,
  59. Consumer: func(event compose.Event) error {
  60. inspected, err := s.apiClient.ContainerInspect(ctx, event.Container)
  61. if err != nil {
  62. return err
  63. }
  64. container := moby.Container{
  65. ID: inspected.ID,
  66. Names: []string{inspected.Name},
  67. Labels: inspected.Config.Labels,
  68. }
  69. name := getContainerNameWithoutProject(container)
  70. if event.Status == "die" {
  71. restarted := watched[container.ID]
  72. watched[container.ID] = restarted + 1
  73. // Container terminated.
  74. willRestart := inspected.HostConfig.RestartPolicy.MaximumRetryCount > restarted
  75. listener(compose.ContainerEvent{
  76. Type: compose.ContainerEventExit,
  77. Container: name,
  78. Service: container.Labels[serviceLabel],
  79. ExitCode: inspected.State.ExitCode,
  80. Restarting: willRestart,
  81. })
  82. if !willRestart {
  83. // we're done with this one
  84. delete(watched, container.ID)
  85. }
  86. if len(watched) == 0 {
  87. // all project containers stopped, we're done
  88. stop()
  89. }
  90. return nil
  91. }
  92. if event.Status == "start" {
  93. count, ok := watched[container.ID]
  94. mustAttach := ok && count > 0 // Container restarted, need to re-attach
  95. if !ok {
  96. // A new container has just been added to service by scale
  97. watched[container.ID] = 0
  98. mustAttach = true
  99. }
  100. if mustAttach {
  101. // Container restarted, need to re-attach
  102. err := s.attachContainer(ctx, container, listener, project)
  103. if err != nil {
  104. return err
  105. }
  106. }
  107. }
  108. return nil
  109. },
  110. })
  111. if errors.Is(ctx.Err(), context.Canceled) {
  112. return nil
  113. }
  114. return err
  115. }