start.go 3.9 KB

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