start.go 3.3 KB

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