From daf97624b98e58a9baf73e530b11852081923a91 Mon Sep 17 00:00:00 2001 From: dergreg <39279568+dergreg@users.noreply.github.com> Date: Tue, 26 Aug 2025 22:59:33 +0200 Subject: [PATCH] Fix Container Health Status Not Displayed Correctly in Dashboard #12319 (#12828) --- api/docker/{ => stats}/container_stats.go | 23 ++++++++---- .../{ => stats}/container_stats_test.go | 8 ++-- api/http/handler/docker/dashboard.go | 16 ++++---- .../docker/DashboardView/ContainerStatus.tsx | 2 +- pkg/snapshot/docker.go | 37 +------------------ 5 files changed, 30 insertions(+), 56 deletions(-) rename api/docker/{ => stats}/container_stats.go (64%) rename api/docker/{ => stats}/container_stats_test.go (72%) diff --git a/api/docker/container_stats.go b/api/docker/stats/container_stats.go similarity index 64% rename from api/docker/container_stats.go rename to api/docker/stats/container_stats.go index 321d2e22c..bf3dc532e 100644 --- a/api/docker/container_stats.go +++ b/api/docker/stats/container_stats.go @@ -1,6 +1,11 @@ -package docker +package stats -import "github.com/docker/docker/api/types" +import ( + "strings" + + "github.com/docker/docker/api/types" + "github.com/rs/zerolog/log" +) type ContainerStats struct { Running int `json:"running"` @@ -13,18 +18,20 @@ type ContainerStats struct { func CalculateContainerStats(containers []types.Container) ContainerStats { var running, stopped, healthy, unhealthy int for _, container := range containers { + log.Debug().Str("containerId", container.ID).Str("state", container.State).Str("status", container.Status).Msg("Container info") + switch container.State { case "running": running++ - case "healthy": - running++ - healthy++ - case "unhealthy": - running++ - unhealthy++ case "exited", "stopped": stopped++ } + + if strings.Contains(container.Status, "(healthy)") { + healthy++ + } else if strings.Contains(container.Status, "(unhealthy)") { + unhealthy++ + } } return ContainerStats{ diff --git a/api/docker/container_stats_test.go b/api/docker/stats/container_stats_test.go similarity index 72% rename from api/docker/container_stats_test.go rename to api/docker/stats/container_stats_test.go index 5422a276c..1c7d5e0a8 100644 --- a/api/docker/container_stats_test.go +++ b/api/docker/stats/container_stats_test.go @@ -1,4 +1,4 @@ -package docker +package stats import ( "testing" @@ -10,11 +10,11 @@ import ( func TestCalculateContainerStats(t *testing.T) { containers := []types.Container{ {State: "running"}, - {State: "running"}, + {State: "running", Status: "Up 5 minutes (healthy)"}, {State: "exited"}, {State: "stopped"}, - {State: "healthy"}, - {State: "unhealthy"}, + {State: "running", Status: "Up 10 minutes"}, + {State: "running", Status: "Up about an hour (unhealthy)"}, } stats := CalculateContainerStats(containers) diff --git a/api/http/handler/docker/dashboard.go b/api/http/handler/docker/dashboard.go index c220106b7..f872ba586 100644 --- a/api/http/handler/docker/dashboard.go +++ b/api/http/handler/docker/dashboard.go @@ -11,7 +11,7 @@ import ( "github.com/docker/docker/api/types/volume" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" - "github.com/portainer/portainer/api/docker" + "github.com/portainer/portainer/api/docker/stats" "github.com/portainer/portainer/api/http/errors" "github.com/portainer/portainer/api/http/handler/docker/utils" "github.com/portainer/portainer/api/http/middlewares" @@ -26,12 +26,12 @@ type imagesCounters struct { } type dashboardResponse struct { - Containers docker.ContainerStats `json:"containers"` - Services int `json:"services"` - Images imagesCounters `json:"images"` - Volumes int `json:"volumes"` - Networks int `json:"networks"` - Stacks int `json:"stacks"` + Containers stats.ContainerStats `json:"containers"` + Services int `json:"services"` + Images imagesCounters `json:"images"` + Volumes int `json:"volumes"` + Networks int `json:"networks"` + Stacks int `json:"stacks"` } // @id dockerDashboard @@ -150,7 +150,7 @@ func (h *Handler) dashboard(w http.ResponseWriter, r *http.Request) *httperror.H Size: totalSize, }, Services: len(services), - Containers: docker.CalculateContainerStats(containers), + Containers: stats.CalculateContainerStats(containers), Networks: len(networks), Volumes: len(volumes), Stacks: stackCount, diff --git a/app/react/docker/DashboardView/ContainerStatus.tsx b/app/react/docker/DashboardView/ContainerStatus.tsx index 2c22bd181..09ee39cdf 100644 --- a/app/react/docker/DashboardView/ContainerStatus.tsx +++ b/app/react/docker/DashboardView/ContainerStatus.tsx @@ -30,7 +30,7 @@ export function ContainerStatus({ stats }: Props) { {stats.healthy} healthy