start.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/compose-spec/compose-go/v2/types"
  20. containerType "github.com/docker/docker/api/types/container"
  21. "github.com/docker/docker/api/types/filters"
  22. "github.com/docker/compose/v5/pkg/api"
  23. )
  24. func (s *composeService) Start(ctx context.Context, projectName string, options api.StartOptions) error {
  25. return Run(ctx, func(ctx context.Context) error {
  26. return s.start(ctx, strings.ToLower(projectName), options, nil)
  27. }, "start", s.events)
  28. }
  29. func (s *composeService) start(ctx context.Context, projectName string, options api.StartOptions, listener api.ContainerEventListener) error {
  30. project := options.Project
  31. if project == nil {
  32. var containers Containers
  33. containers, err := s.getContainers(ctx, projectName, oneOffExclude, true)
  34. if err != nil {
  35. return err
  36. }
  37. project, err = s.projectFromName(containers, projectName, options.AttachTo...)
  38. if err != nil {
  39. return err
  40. }
  41. }
  42. var containers Containers
  43. containers, err := s.apiClient().ContainerList(ctx, containerType.ListOptions{
  44. Filters: filters.NewArgs(
  45. projectFilter(project.Name),
  46. oneOffFilter(false),
  47. ),
  48. All: true,
  49. })
  50. if err != nil {
  51. return err
  52. }
  53. err = InDependencyOrder(ctx, project, func(c context.Context, name string) error {
  54. service, err := project.GetService(name)
  55. if err != nil {
  56. return err
  57. }
  58. return s.startService(ctx, project, service, containers, listener, options.WaitTimeout)
  59. })
  60. if err != nil {
  61. return err
  62. }
  63. if options.Wait {
  64. depends := types.DependsOnConfig{}
  65. for _, s := range project.Services {
  66. depends[s.Name] = types.ServiceDependency{
  67. Condition: getDependencyCondition(s, project),
  68. Required: true,
  69. }
  70. }
  71. if options.WaitTimeout > 0 {
  72. withTimeout, cancel := context.WithTimeout(ctx, options.WaitTimeout)
  73. ctx = withTimeout
  74. defer cancel()
  75. }
  76. err = s.waitDependencies(ctx, project, project.Name, depends, containers, 0)
  77. if err != nil {
  78. if errors.Is(ctx.Err(), context.DeadlineExceeded) {
  79. return fmt.Errorf("application not healthy after %s", options.WaitTimeout)
  80. }
  81. return err
  82. }
  83. }
  84. return nil
  85. }
  86. // getDependencyCondition checks if service is depended on by other services
  87. // with service_completed_successfully condition, and applies that condition
  88. // instead, or --wait will never finish waiting for one-shot containers
  89. func getDependencyCondition(service types.ServiceConfig, project *types.Project) string {
  90. for _, services := range project.Services {
  91. for dependencyService, dependencyConfig := range services.DependsOn {
  92. if dependencyService == service.Name && dependencyConfig.Condition == types.ServiceConditionCompletedSuccessfully {
  93. return types.ServiceConditionCompletedSuccessfully
  94. }
  95. }
  96. }
  97. return ServiceConditionRunningOrHealthy
  98. }