Skip to content

Commit 4c8f9eb

Browse files
container/ps: add HealthCheck formatter field
Signed-off-by: Mohammed Thaha <mohammedthahacse@gmail.com>
1 parent 9d7ad9f commit 4c8f9eb

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

cli/command/formatter/container.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
localVolumes = "LOCAL VOLUMES"
2929
networksHeader = "NETWORKS"
3030
platformHeader = "PLATFORM"
31+
healthCheckHeader = "HEALTHCHECK"
3132
)
3233

3334
// Platform wraps a [ocispec.Platform] to implement the stringer interface.
@@ -121,6 +122,7 @@ func NewContainerContext() *ContainerContext {
121122
"LocalVolumes": localVolumes,
122123
"Networks": networksHeader,
123124
"Platform": platformHeader,
125+
"HealthCheck": healthCheckHeader,
124126
}
125127
return &containerCtx
126128
}
@@ -352,6 +354,27 @@ func (c *ContainerContext) Networks() string {
352354
return strings.Join(networks, ",")
353355
}
354356

357+
// HealthCheck returns the container's health status (for example, "healthy","unhealthy", or "starting").
358+
// If no healthcheck is configured, an empty
359+
// string is returned.
360+
func (c *ContainerContext) HealthCheck() string {
361+
if c.c.Health != nil && c.c.Health.Status != "" {
362+
return string(c.c.Health.Status)
363+
}
364+
365+
// Fallback for daemons/API versions that include health only in Status text.
366+
switch {
367+
case strings.HasSuffix(c.c.Status, "(healthy)"):
368+
return string(container.Healthy)
369+
case strings.HasSuffix(c.c.Status, "(unhealthy)"):
370+
return string(container.Unhealthy)
371+
case strings.HasSuffix(c.c.Status, "(health: starting)"):
372+
return string(container.Starting)
373+
}
374+
375+
return ""
376+
}
377+
355378
// DisplayablePorts returns formatted string representing open ports of container
356379
// e.g. "0.0.0.0:80->9090/tcp, 9988/tcp"
357380
// it's used by command 'docker ps'

cli/command/formatter/container_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
494494
{
495495
"Command": `""`,
496496
"CreatedAt": expectedCreated,
497+
"HealthCheck": "",
497498
"ID": "containerID1",
498499
"Image": "ubuntu",
499500
"Labels": "",
@@ -511,6 +512,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
511512
{
512513
"Command": `""`,
513514
"CreatedAt": expectedCreated,
515+
"HealthCheck": "",
514516
"ID": "containerID2",
515517
"Image": "ubuntu",
516518
"Labels": "",
@@ -528,6 +530,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
528530
{
529531
"Command": `""`,
530532
"CreatedAt": expectedCreated,
533+
"HealthCheck": "",
531534
"ID": "containerID3",
532535
"Image": "ubuntu",
533536
"Labels": "",
@@ -615,6 +618,7 @@ func TestContainerBackCompat(t *testing.T) {
615618
{field: "Image", expected: "docker.io/library/ubuntu"},
616619
{field: "Command", expected: `"/bin/sh"`},
617620
{field: "CreatedAt", expected: time.Unix(createdAtTime.Unix(), 0).String()},
621+
{field: "HealthCheck", expected: ""},
618622
{field: "RunningFor", expected: "12 months ago"},
619623
{field: "Ports", expected: "8080/tcp"},
620624
{field: "Status", expected: "running"},

docs/reference/commandline/container_ls.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ Valid placeholders for the Go template are listed below:
405405
| `.Ports` | Exposed ports. |
406406
| `.State` | Container status (for example; "created", "running", "exited"). |
407407
| `.Status` | Container status with details about duration and health-status. |
408+
| `.HealthCheck`| Container health status ("starting", "healthy", "unhealthy", or "none"; empty when unavailable).|
408409
| `.Size` | Container disk size. |
409410
| `.Names` | Container names. |
410411
| `.Labels` | All labels assigned to the container. |

0 commit comments

Comments
 (0)