start.go 3.4 KB

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