start.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "errors"
  17. "fmt"
  18. "strings"
  19. "github.com/docker/compose/v2/pkg/api"
  20. "github.com/docker/compose/v2/pkg/progress"
  21. containerType "github.com/docker/docker/api/types/container"
  22. "github.com/compose-spec/compose-go/v2/types"
  23. "github.com/docker/docker/api/types/filters"
  24. "golang.org/x/sync/errgroup"
  25. )
  26. func (s *composeService) Start(ctx context.Context, projectName string, options api.StartOptions) error {
  27. return progress.Run(ctx, func(ctx context.Context) error {
  28. return s.start(ctx, strings.ToLower(projectName), options, nil)
  29. }, s.stdinfo())
  30. }
  31. func (s *composeService) start(ctx context.Context, projectName string, options api.StartOptions, listener api.ContainerEventListener) error {
  32. project := options.Project
  33. if project == nil {
  34. var containers Containers
  35. containers, err := s.getContainers(ctx, projectName, oneOffExclude, true)
  36. if err != nil {
  37. return err
  38. }
  39. project, err = s.projectFromName(containers, projectName, options.AttachTo...)
  40. if err != nil {
  41. return err
  42. }
  43. }
  44. // use an independent context tied to the errgroup for background attach operations
  45. // the primary context is still used for other operations
  46. // this means that once any attach operation fails, all other attaches are cancelled,
  47. // but an attach failing won't interfere with the rest of the start
  48. eg, attachCtx := errgroup.WithContext(ctx)
  49. if listener != nil {
  50. _, err := s.attach(attachCtx, project, listener, options.AttachTo)
  51. if err != nil {
  52. return err
  53. }
  54. }
  55. var containers Containers
  56. containers, err := s.apiClient().ContainerList(ctx, containerType.ListOptions{
  57. Filters: filters.NewArgs(
  58. projectFilter(project.Name),
  59. oneOffFilter(false),
  60. ),
  61. All: true,
  62. })
  63. if err != nil {
  64. return err
  65. }
  66. err = InDependencyOrder(ctx, project, func(c context.Context, name string) error {
  67. service, err := project.GetService(name)
  68. if err != nil {
  69. return err
  70. }
  71. return s.startService(ctx, project, service, containers, listener, options.WaitTimeout)
  72. })
  73. if err != nil {
  74. return err
  75. }
  76. if options.Wait {
  77. depends := types.DependsOnConfig{}
  78. for _, s := range project.Services {
  79. depends[s.Name] = types.ServiceDependency{
  80. Condition: getDependencyCondition(s, project),
  81. Required: true,
  82. }
  83. }
  84. if options.WaitTimeout > 0 {
  85. withTimeout, cancel := context.WithTimeout(ctx, options.WaitTimeout)
  86. ctx = withTimeout
  87. defer cancel()
  88. }
  89. err = s.waitDependencies(ctx, project, project.Name, depends, containers, 0)
  90. if err != nil {
  91. if errors.Is(ctx.Err(), context.DeadlineExceeded) {
  92. return fmt.Errorf("application not healthy after %s", options.WaitTimeout)
  93. }
  94. return err
  95. }
  96. }
  97. return eg.Wait()
  98. }
  99. // getDependencyCondition checks if service is depended on by other services
  100. // with service_completed_successfully condition, and applies that condition
  101. // instead, or --wait will never finish waiting for one-shot containers
  102. func getDependencyCondition(service types.ServiceConfig, project *types.Project) string {
  103. for _, services := range project.Services {
  104. for dependencyService, dependencyConfig := range services.DependsOn {
  105. if dependencyService == service.Name && dependencyConfig.Condition == types.ServiceConditionCompletedSuccessfully {
  106. return types.ServiceConditionCompletedSuccessfully
  107. }
  108. }
  109. }
  110. return ServiceConditionRunningOrHealthy
  111. }