@@ -290,8 +290,8 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
290
290
continue
291
291
}
292
292
293
- containers = containers .filter (isService (dep ))
294
- w .Events (containerEvents (containers , progress .Waiting ))
293
+ waitingFor : = containers .filter (isService (dep ))
294
+ w .Events (containerEvents (waitingFor , progress .Waiting ))
295
295
296
296
dep , config := dep , config
297
297
eg .Go (func () error {
@@ -301,31 +301,31 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
301
301
<- ticker .C
302
302
switch config .Condition {
303
303
case ServiceConditionRunningOrHealthy :
304
- healthy , err := s .isServiceHealthy (ctx , containers , dep , true )
304
+ healthy , err := s .isServiceHealthy (ctx , waitingFor , true )
305
305
if err != nil {
306
306
return err
307
307
}
308
308
if healthy {
309
- w .Events (containerEvents (containers , progress .Healthy ))
309
+ w .Events (containerEvents (waitingFor , progress .Healthy ))
310
310
return nil
311
311
}
312
312
case types .ServiceConditionHealthy :
313
- healthy , err := s .isServiceHealthy (ctx , containers , dep , false )
313
+ healthy , err := s .isServiceHealthy (ctx , waitingFor , false )
314
314
if err != nil {
315
- w .Events (containerEvents (containers , progress .ErrorEvent ))
315
+ w .Events (containerEvents (waitingFor , progress .ErrorEvent ))
316
316
return errors .Wrap (err , "dependency failed to start" )
317
317
}
318
318
if healthy {
319
- w .Events (containerEvents (containers , progress .Healthy ))
319
+ w .Events (containerEvents (waitingFor , progress .Healthy ))
320
320
return nil
321
321
}
322
322
case types .ServiceConditionCompletedSuccessfully :
323
- exited , code , err := s .isServiceCompleted (ctx , containers , dep )
323
+ exited , code , err := s .isServiceCompleted (ctx , waitingFor )
324
324
if err != nil {
325
325
return err
326
326
}
327
327
if exited {
328
- w .Events (containerEvents (containers , progress .Exited ))
328
+ w .Events (containerEvents (waitingFor , progress .Exited ))
329
329
if code != 0 {
330
330
return fmt .Errorf ("service %q didn't complete successfully: exit %d" , dep , code )
331
331
}
@@ -646,42 +646,41 @@ func (s *composeService) connectContainerToNetwork(ctx context.Context, id strin
646
646
return nil
647
647
}
648
648
649
- func (s * composeService ) isServiceHealthy (ctx context.Context , containers Containers , service string , fallbackRunning bool ) (bool , error ) {
650
- if len (containers ) == 0 {
651
- return false , nil
652
- }
649
+ func (s * composeService ) isServiceHealthy (ctx context.Context , containers Containers , fallbackRunning bool ) (bool , error ) {
653
650
for _ , c := range containers {
654
651
container , err := s .apiClient ().ContainerInspect (ctx , c .ID )
655
652
if err != nil {
656
653
return false , err
657
654
}
655
+ name := container .Name [1 :]
656
+
657
+ if container .State .Status == "exited" {
658
+ return false , fmt .Errorf ("container %s exited (%d)" , name , container .State .ExitCode )
659
+ }
660
+
658
661
if container .Config .Healthcheck == nil && fallbackRunning {
659
662
// Container does not define a health check, but we can fall back to "running" state
660
663
return container .State != nil && container .State .Status == "running" , nil
661
664
}
662
665
663
- if container .State .Status == "exited" {
664
- return false , fmt .Errorf ("container for service %q exited (%d)" , service , container .State .ExitCode )
665
- }
666
-
667
666
if container .State == nil || container .State .Health == nil {
668
- return false , fmt .Errorf ("container for service %q has no healthcheck configured" , service )
667
+ return false , fmt .Errorf ("container %s has no healthcheck configured" , name )
669
668
}
670
669
switch container .State .Health .Status {
671
670
case moby .Healthy :
672
671
// Continue by checking the next container.
673
672
case moby .Unhealthy :
674
- return false , fmt .Errorf ("container for service %q is unhealthy" , service )
673
+ return false , fmt .Errorf ("container %s is unhealthy" , name )
675
674
case moby .Starting :
676
675
return false , nil
677
676
default :
678
- return false , fmt .Errorf ("container for service %q had unexpected health status %q" , service , container .State .Health .Status )
677
+ return false , fmt .Errorf ("container %s had unexpected health status %q" , name , container .State .Health .Status )
679
678
}
680
679
}
681
680
return true , nil
682
681
}
683
682
684
- func (s * composeService ) isServiceCompleted (ctx context.Context , containers Containers , dep string ) (bool , int , error ) {
683
+ func (s * composeService ) isServiceCompleted (ctx context.Context , containers Containers ) (bool , int , error ) {
685
684
for _ , c := range containers {
686
685
container , err := s .apiClient ().ContainerInspect (ctx , c .ID )
687
686
if err != nil {
@@ -712,7 +711,7 @@ func (s *composeService) startService(ctx context.Context, project *types.Projec
712
711
}
713
712
714
713
w := progress .ContextWriter (ctx )
715
- for _ , container := range containers {
714
+ for _ , container := range containers . filter ( isService ( service . Name )) {
716
715
if container .State == ContainerRunning {
717
716
continue
718
717
}
0 commit comments