MCPcopy Index your code
hub / github.com/ddev/ddev / GetContainerHealth

Function GetContainerHealth

pkg/dockerutil/containers.go:470–527  ·  view source on GitHub ↗

GetContainerHealth retrieves the health status of a given container. returns status, most-recent-log The container is only considered "healthy" if it's also "running", contrary to Docker's normal usage

(c *container.Summary)

Source from the content-addressed store, hash-verified

468// returns status, most-recent-log
469// The container is only considered "healthy" if it's also "running", contrary to Docker's normal usage
470func GetContainerHealth(c *container.Summary) (string, string) {
471 if c == nil {
472 return "no container", ""
473 }
474
475 // If the container is not running, then return exited as the health.
476 // "exited" means stopped.
477 cState := string(c.State)
478 if cState == string(container.StateExited) || cState == string(container.StateRestarting) {
479 return cState, ""
480 }
481
482 ctx, apiClient, err := GetDockerClient()
483 if err != nil {
484 return "", ""
485 }
486 inspect, err := apiClient.ContainerInspect(ctx, c.ID, client.ContainerInspectOptions{})
487 if err != nil {
488 output.UserOut.Warnf("Error getting container to inspect: %v", err)
489 return "", ""
490 }
491
492 logOutput := ""
493 status := ""
494 if inspect.Container.State.Health != nil {
495 status = string(inspect.Container.State.Health.Status)
496 }
497 // The last log is the most recent
498 if status != "" {
499 numLogs := len(inspect.Container.State.Health.Log)
500 if numLogs > 0 {
501 logOutput = fmt.Sprintf("%v", inspect.Container.State.Health.Log[numLogs-1].Output)
502 }
503 // A container can't be healthy if it's not running.
504 // Docker/Podman may cache the last health status even after state changes.
505 if inspect.Container.State.Status != container.StateRunning {
506 // Return actual state for known non-running states
507 switch inspect.Container.State.Status {
508 case container.StateExited, container.StateRestarting:
509 status = string(inspect.Container.State.Status)
510 default:
511 // For other non-running states, override cached health to unhealthy
512 status = string(container.Unhealthy)
513 }
514 }
515 } else {
516 // Some containers may not have a healthcheck. In that case
517 // we use State to determine health
518 switch inspect.Container.State.Status {
519 case container.StateRunning:
520 status = string(container.Healthy)
521 case container.StateExited:
522 status = string(container.StateExited)
523 }
524 }
525
526 return status, strings.TrimSpace(logOutput)
527}

Callers 8

GetRouterStatusFunction · 0.92
GetErrLogsFromAppFunction · 0.92
GetSSHAuthStatusFunction · 0.92
SiteStatusMethod · 0.92
TestGetContainerHealthFunction · 0.92
ContainerWaitFunction · 0.85
ContainersWaitFunction · 0.85

Calls 2

GetDockerClientFunction · 0.85
WarnfMethod · 0.80

Tested by 1

TestGetContainerHealthFunction · 0.74