@@ -28,7 +28,6 @@ import (
2828 "github.com/containerd/containerd/platforms"
2929 moby "github.com/docker/docker/api/types"
3030 containerType "github.com/docker/docker/api/types/container"
31- "github.com/docker/docker/api/types/filters"
3231 "github.com/docker/docker/api/types/network"
3332 specs "github.com/opencontainers/image-spec/specs-go/v1"
3433 "github.com/pkg/errors"
@@ -281,7 +280,7 @@ func containerEvents(containers Containers, eventFunc func(string) progress.Even
281280// ServiceConditionRunningOrHealthy is a service condition on status running or healthy
282281const ServiceConditionRunningOrHealthy = "running_or_healthy"
283282
284- func (s * composeService ) waitDependencies (ctx context.Context , project * types.Project , dependencies types.DependsOnConfig ) error {
283+ func (s * composeService ) waitDependencies (ctx context.Context , project * types.Project , dependencies types.DependsOnConfig , containers Containers ) error {
285284 eg , _ := errgroup .WithContext (ctx )
286285 w := progress .ContextWriter (ctx )
287286 for dep , config := range dependencies {
@@ -291,10 +290,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
291290 continue
292291 }
293292
294- containers , err := s .getContainers (ctx , project .Name , oneOffExclude , false , dep )
295- if err != nil {
296- return err
297- }
293+ containers = containers .filter (isService (dep ))
298294 w .Events (containerEvents (containers , progress .Waiting ))
299295
300296 dep , config := dep , config
@@ -305,7 +301,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
305301 <- ticker .C
306302 switch config .Condition {
307303 case ServiceConditionRunningOrHealthy :
308- healthy , err := s .isServiceHealthy (ctx , project , dep , true )
304+ healthy , err := s .isServiceHealthy (ctx , containers , dep , true )
309305 if err != nil {
310306 return err
311307 }
@@ -314,7 +310,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
314310 return nil
315311 }
316312 case types .ServiceConditionHealthy :
317- healthy , err := s .isServiceHealthy (ctx , project , dep , false )
313+ healthy , err := s .isServiceHealthy (ctx , containers , dep , false )
318314 if err != nil {
319315 w .Events (containerEvents (containers , progress .ErrorEvent ))
320316 return errors .Wrap (err , "dependency failed to start" )
@@ -324,7 +320,7 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
324320 return nil
325321 }
326322 case types .ServiceConditionCompletedSuccessfully :
327- exited , code , err := s .isServiceCompleted (ctx , project , dep )
323+ exited , code , err := s .isServiceCompleted (ctx , containers , dep )
328324 if err != nil {
329325 return err
330326 }
@@ -650,12 +646,7 @@ func (s *composeService) connectContainerToNetwork(ctx context.Context, id strin
650646 return nil
651647}
652648
653- func (s * composeService ) isServiceHealthy (ctx context.Context , project * types.Project , service string , fallbackRunning bool ) (bool , error ) {
654- containers , err := s .getContainers (ctx , project .Name , oneOffExclude , true , service )
655- if err != nil {
656- return false , err
657- }
658-
649+ func (s * composeService ) isServiceHealthy (ctx context.Context , containers Containers , service string , fallbackRunning bool ) (bool , error ) {
659650 if len (containers ) == 0 {
660651 return false , nil
661652 }
@@ -690,11 +681,7 @@ func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Pr
690681 return true , nil
691682}
692683
693- func (s * composeService ) isServiceCompleted (ctx context.Context , project * types.Project , dep string ) (bool , int , error ) {
694- containers , err := s .getContainers (ctx , project .Name , oneOffExclude , true , dep )
695- if err != nil {
696- return false , 0 , err
697- }
684+ func (s * composeService ) isServiceCompleted (ctx context.Context , containers Containers , dep string ) (bool , int , error ) {
698685 for _ , c := range containers {
699686 container , err := s .apiClient ().ContainerInspect (ctx , c .ID )
700687 if err != nil {
@@ -707,23 +694,12 @@ func (s *composeService) isServiceCompleted(ctx context.Context, project *types.
707694 return false , 0 , nil
708695}
709696
710- func (s * composeService ) startService (ctx context.Context , project * types.Project , service types.ServiceConfig ) error {
697+ func (s * composeService ) startService (ctx context.Context , project * types.Project , service types.ServiceConfig , containers Containers ) error {
711698 if service .Deploy != nil && service .Deploy .Replicas != nil && * service .Deploy .Replicas == 0 {
712699 return nil
713700 }
714701
715- err := s .waitDependencies (ctx , project , service .DependsOn )
716- if err != nil {
717- return err
718- }
719- containers , err := s .apiClient ().ContainerList (ctx , moby.ContainerListOptions {
720- Filters : filters .NewArgs (
721- projectFilter (project .Name ),
722- serviceFilter (service .Name ),
723- oneOffFilter (false ),
724- ),
725- All : true ,
726- })
702+ err := s .waitDependencies (ctx , project , service .DependsOn , containers )
727703 if err != nil {
728704 return err
729705 }
0 commit comments