Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 8d8d405

Browse files
Mohammed-ThahathaJeztah
authored andcommitted
container/ps: add HealthStatus formatter field
Signed-off-by: Mohammed Thaha <mohammedthahacse@gmail.com> Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 517ca50 commit 8d8d405

3 files changed

Lines changed: 43 additions & 7 deletions

File tree

cli/command/formatter/container.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ import (
2121
const (
2222
defaultContainerTableFormat = "table {{.ID}}\t{{.Image}}\t{{.Command}}\t{{.RunningFor}}\t{{.Status}}\t{{.Ports}}\t{{.Names}}"
2323

24-
namesHeader = "NAMES"
25-
commandHeader = "COMMAND"
26-
runningForHeader = "CREATED"
27-
mountsHeader = "MOUNTS"
28-
localVolumes = "LOCAL VOLUMES"
29-
networksHeader = "NETWORKS"
30-
platformHeader = "PLATFORM"
24+
namesHeader = "NAMES"
25+
commandHeader = "COMMAND"
26+
runningForHeader = "CREATED"
27+
mountsHeader = "MOUNTS"
28+
localVolumes = "LOCAL VOLUMES"
29+
networksHeader = "NETWORKS"
30+
platformHeader = "PLATFORM"
31+
healthStatusHeader = "HEALTH STATUS"
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+
"HealthStatus": healthStatusHeader,
124126
}
125127
return &containerCtx
126128
}
@@ -352,6 +354,35 @@ func (c *ContainerContext) Networks() string {
352354
return strings.Join(networks, ",")
353355
}
354356

357+
// HealthStatus 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) HealthStatus() string {
361+
if c.c.Health != nil && c.c.Health.Status != "" {
362+
return string(c.c.Health.Status)
363+
}
364+
365+
// Fallback for API versions before v1.52, which include health only in Status text;
366+
// see https://github.com/moby/moby/pull/50281
367+
// see https://github.com/moby/moby/blob/docker-v29.4.3/daemon/container/health.go#L18-L43
368+
_, health, ok := strings.Cut(c.c.Status, "(")
369+
if !ok || !strings.HasSuffix(health, ")") {
370+
return ""
371+
}
372+
373+
health = strings.TrimSuffix(health, ")")
374+
health = strings.TrimPrefix(health, "health: ")
375+
376+
switch container.HealthStatus(health) {
377+
case container.Healthy, container.Unhealthy, container.Starting:
378+
return health
379+
case container.NoHealthcheck:
380+
return ""
381+
default:
382+
return ""
383+
}
384+
}
385+
355386
// DisplayablePorts returns formatted string representing open ports of container
356387
// e.g. "0.0.0.0:80->9090/tcp, 9988/tcp"
357388
// 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+
"HealthStatus": "",
497498
"ID": "containerID1",
498499
"Image": "ubuntu",
499500
"Labels": "",
@@ -511,6 +512,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
511512
{
512513
"Command": `""`,
513514
"CreatedAt": expectedCreated,
515+
"HealthStatus": "",
514516
"ID": "containerID2",
515517
"Image": "ubuntu",
516518
"Labels": "",
@@ -528,6 +530,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
528530
{
529531
"Command": `""`,
530532
"CreatedAt": expectedCreated,
533+
"HealthStatus": "",
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: "HealthStatus", 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+
| `.HealthStatus` | Container health status ("starting", "healthy", "unhealthy"; 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)