@@ -286,9 +286,18 @@ func containerEvents(containers Containers, eventFunc func(string) progress.Even
286286 return events
287287}
288288
289+ func containerSkippedEvents (containers Containers , eventFunc func (string , string ) progress.Event , reason string ) []progress.Event {
290+ events := []progress.Event {}
291+ for _ , container := range containers {
292+ events = append (events , eventFunc (getContainerProgressName (container ), reason ))
293+ }
294+ return events
295+ }
296+
289297// ServiceConditionRunningOrHealthy is a service condition on status running or healthy
290298const ServiceConditionRunningOrHealthy = "running_or_healthy"
291299
300+ //nolint:gocyclo
292301func (s * composeService ) waitDependencies (ctx context.Context , project * types.Project , dependencies types.DependsOnConfig , containers Containers ) error {
293302 eg , _ := errgroup .WithContext (ctx )
294303 w := progress .ContextWriter (ctx )
@@ -312,6 +321,11 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
312321 case ServiceConditionRunningOrHealthy :
313322 healthy , err := s .isServiceHealthy (ctx , waitingFor , true )
314323 if err != nil {
324+ if ! config .Required {
325+ w .Events (containerSkippedEvents (waitingFor , progress .SkippedEvent , fmt .Sprintf ("optional dependency %q is not running or is unhealthy" , dep )))
326+ logrus .Warnf ("optional dependency %q is not running or is unhealthy: %s" , dep , err .Error ())
327+ return nil
328+ }
315329 return err
316330 }
317331 if healthy {
@@ -321,6 +335,11 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
321335 case types .ServiceConditionHealthy :
322336 healthy , err := s .isServiceHealthy (ctx , waitingFor , false )
323337 if err != nil {
338+ if ! config .Required {
339+ w .Events (containerSkippedEvents (waitingFor , progress .SkippedEvent , fmt .Sprintf ("optional dependency %q failed to start" , dep )))
340+ logrus .Warnf ("optional dependency %q failed to start: %s" , dep , err .Error ())
341+ return nil
342+ }
324343 w .Events (containerEvents (waitingFor , progress .ErrorEvent ))
325344 return errors .Wrap (err , "dependency failed to start" )
326345 }
@@ -334,6 +353,12 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
334353 return err
335354 }
336355 if exited {
356+ logMessageSuffix := fmt .Sprintf ("%q didn't complete successfully: exit %d" , dep , code )
357+ if ! config .Required {
358+ w .Events (containerSkippedEvents (waitingFor , progress .SkippedEvent , fmt .Sprintf ("optional dependency %s" , logMessageSuffix )))
359+ logrus .Warnf ("optional dependency %s" , logMessageSuffix )
360+ return nil
361+ }
337362 w .Events (containerEvents (waitingFor , progress .Exited ))
338363 if code != 0 {
339364 return fmt .Errorf ("service %q didn't complete successfully: exit %d" , dep , code )
0 commit comments