start.go 4.0 KB

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