start.go 3.6 KB

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