Skip to content

Commit 7c2e8b2

Browse files
Address review feedback
1 parent 7967724 commit 7c2e8b2

3 files changed

Lines changed: 32 additions & 25 deletions

File tree

cli/command/formatter/container.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +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"
31-
healthCheckHeader = "HEALTHCHECK"
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"
3232
)
3333

3434
// Platform wraps a [ocispec.Platform] to implement the stringer interface.
@@ -122,7 +122,7 @@ func NewContainerContext() *ContainerContext {
122122
"LocalVolumes": localVolumes,
123123
"Networks": networksHeader,
124124
"Platform": platformHeader,
125-
"HealthCheck": healthCheckHeader,
125+
"HealthStatus": healthStatusHeader,
126126
}
127127
return &containerCtx
128128
}
@@ -354,25 +354,32 @@ func (c *ContainerContext) Networks() string {
354354
return strings.Join(networks, ",")
355355
}
356356

357-
// HealthCheck returns the container's health status (for example, "healthy","unhealthy", or "starting").
357+
// HealthStatus returns the container's health status (for example, "healthy","unhealthy", or "starting").
358358
// If no healthcheck is configured, an empty
359359
// string is returned.
360-
func (c *ContainerContext) HealthCheck() string {
360+
func (c *ContainerContext) HealthStatus() string {
361361
if c.c.Health != nil && c.c.Health.Status != "" {
362362
return string(c.c.Health.Status)
363363
}
364364

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)
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 ""
373371
}
374372

375-
return ""
373+
health = strings.TrimSuffix(health, ")")
374+
health = strings.TrimPrefix(health, "health: ")
375+
376+
parsedHealth := container.HealthStatus(health)
377+
switch parsedHealth {
378+
case container.Healthy, container.Unhealthy, container.Starting:
379+
return health
380+
default:
381+
return ""
382+
}
376383
}
377384

378385
// DisplayablePorts returns formatted string representing open ports of container

cli/command/formatter/container_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
494494
{
495495
"Command": `""`,
496496
"CreatedAt": expectedCreated,
497-
"HealthCheck": "",
497+
"HealthStatus": "",
498498
"ID": "containerID1",
499499
"Image": "ubuntu",
500500
"Labels": "",
@@ -512,7 +512,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
512512
{
513513
"Command": `""`,
514514
"CreatedAt": expectedCreated,
515-
"HealthCheck": "",
515+
"HealthStatus": "",
516516
"ID": "containerID2",
517517
"Image": "ubuntu",
518518
"Labels": "",
@@ -530,7 +530,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
530530
{
531531
"Command": `""`,
532532
"CreatedAt": expectedCreated,
533-
"HealthCheck": "",
533+
"HealthStatus": "",
534534
"ID": "containerID3",
535535
"Image": "ubuntu",
536536
"Labels": "",
@@ -618,7 +618,7 @@ func TestContainerBackCompat(t *testing.T) {
618618
{field: "Image", expected: "docker.io/library/ubuntu"},
619619
{field: "Command", expected: `"/bin/sh"`},
620620
{field: "CreatedAt", expected: time.Unix(createdAtTime.Unix(), 0).String()},
621-
{field: "HealthCheck", expected: ""},
621+
{field: "HealthStatus", expected: ""},
622622
{field: "RunningFor", expected: "12 months ago"},
623623
{field: "Ports", expected: "8080/tcp"},
624624
{field: "Status", expected: "running"},

docs/reference/commandline/container_ls.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +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).|
408+
| `.HealthStatus` | Container health status ("starting", "healthy", "unhealthy"; empty when unavailable). |
409409
| `.Size` | Container disk size. |
410410
| `.Names` | Container names. |
411411
| `.Labels` | All labels assigned to the container. |

0 commit comments

Comments
 (0)