Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cli/command/formatter/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
localVolumes = "LOCAL VOLUMES"
networksHeader = "NETWORKS"
platformHeader = "PLATFORM"
healthCheckHeader = "HEALTHCHECK"
)

// Platform wraps a [ocispec.Platform] to implement the stringer interface.
Expand Down Expand Up @@ -121,6 +122,7 @@ func NewContainerContext() *ContainerContext {
"LocalVolumes": localVolumes,
"Networks": networksHeader,
"Platform": platformHeader,
"HealthCheck": healthCheckHeader,
}
return &containerCtx
}
Expand Down Expand Up @@ -352,6 +354,27 @@ func (c *ContainerContext) Networks() string {
return strings.Join(networks, ",")
}

// HealthCheck returns the container's health status (for example, "healthy","unhealthy", or "starting").
// If no healthcheck is configured, an empty
// string is returned.
func (c *ContainerContext) HealthCheck() string {
if c.c.Health != nil && c.c.Health.Status != "" {
return string(c.c.Health.Status)
}

// Fallback for daemons/API versions that include health only in Status text.
switch {
case strings.HasSuffix(c.c.Status, "(healthy)"):
return string(container.Healthy)
case strings.HasSuffix(c.c.Status, "(unhealthy)"):
return string(container.Unhealthy)
case strings.HasSuffix(c.c.Status, "(health: starting)"):
return string(container.Starting)
}

return ""
}

// DisplayablePorts returns formatted string representing open ports of container
// e.g. "0.0.0.0:80->9090/tcp, 9988/tcp"
// it's used by command 'docker ps'
Expand Down
4 changes: 4 additions & 0 deletions cli/command/formatter/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
{
"Command": `""`,
"CreatedAt": expectedCreated,
"HealthCheck": "",
"ID": "containerID1",
"Image": "ubuntu",
"Labels": "",
Expand All @@ -511,6 +512,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
{
"Command": `""`,
"CreatedAt": expectedCreated,
"HealthCheck": "",
"ID": "containerID2",
"Image": "ubuntu",
"Labels": "",
Expand All @@ -528,6 +530,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
{
"Command": `""`,
"CreatedAt": expectedCreated,
"HealthCheck": "",
"ID": "containerID3",
"Image": "ubuntu",
"Labels": "",
Expand Down Expand Up @@ -615,6 +618,7 @@ func TestContainerBackCompat(t *testing.T) {
{field: "Image", expected: "docker.io/library/ubuntu"},
{field: "Command", expected: `"/bin/sh"`},
{field: "CreatedAt", expected: time.Unix(createdAtTime.Unix(), 0).String()},
{field: "HealthCheck", expected: ""},
{field: "RunningFor", expected: "12 months ago"},
{field: "Ports", expected: "8080/tcp"},
{field: "Status", expected: "running"},
Expand Down
1 change: 1 addition & 0 deletions docs/reference/commandline/container_ls.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ Valid placeholders for the Go template are listed below:
| `.Ports` | Exposed ports. |
| `.State` | Container status (for example; "created", "running", "exited"). |
| `.Status` | Container status with details about duration and health-status. |
| `.HealthCheck`| Container health status ("starting", "healthy", "unhealthy", or "none"; empty when unavailable).|
| `.Size` | Container disk size. |
| `.Names` | Container names. |
| `.Labels` | All labels assigned to the container. |
Expand Down