Skip to content

Commit 67c0be4

Browse files
committed
docker ps: allow formatting as JSON
With this patch: docker ps --format 'table {{.Names}}\t{{.Platform}}' NAMES PLATFORM optimistic_nightingale linux/arm64 mycontainer linux/arm64/v8 trusting_goldstine linux/arm64 docker ps --format '{{.Platform}}' linux/arm64 linux/arm64/v8 linux/arm64 docker ps --format '{{json .Platform}}' {"architecture":"arm64","os":"linux"} {"architecture":"arm64","os":"linux","variant":"v8"} {"architecture":"arm64","os":"linux"} docker ps --format 'json' {"Command":"\"/bin/bash\"","CreatedAt":"2025-05-13 10:12:19 +0000 UTC","ID":"e8b3b2d604f1","Image":"docker-cli-dev","Labels":"desktop.docker.io/binds/0/Source=/Users/thajeztah/go/src/github.com/docker/cli,desktop.docker.io/binds/0/SourceKind=hostFile,desktop.docker.io/binds/0/Target=/go/src/github.com/docker/cli,desktop.docker.io/mounts/0/Source=/var/run/docker.sock,desktop.docker.io/mounts/0/SourceKind=dockerSocketProxied,desktop.docker.io/mounts/0/Target=/var/run/docker.sock,desktop.docker.io/ports.scheme=v2","LocalVolumes":"1","Mounts":"/host_mnt/User…,docker-cli-dev…,/run/host-serv…","Names":"optimistic_nightingale","Networks":"bridge","Platform":{"architecture":"arm64","os":"linux"},"Ports":"","RunningFor":"38 minutes ago","Size":"0B","State":"running","Status":"Up 38 minutes"} {"Command":"\"/docker-entrypoint.…\"","CreatedAt":"2025-05-13 09:58:01 +0000 UTC","ID":"c93b808dd54e","Image":"nginx:alpine","Labels":"desktop.docker.io/ports.scheme=v2,maintainer=NGINX Docker Maintainers \[email protected]\u003e","LocalVolumes":"0","Mounts":"","Names":"mycontainer","Networks":"bridge","Platform":{"architecture":"arm64","os":"linux","variant":"v8"},"Ports":"80/tcp","RunningFor":"53 minutes ago","Size":"0B","State":"running","Status":"Up 53 minutes"} {"Command":"\"/usr/bin/gotty --ti…\"","CreatedAt":"2025-05-13 07:31:18 +0000 UTC","ID":"cbb981b06e46","Image":"thajeztah/dockershell:latest","Labels":"desktop.docker.io/ports.scheme=v2,com.thajeztah.docker-shell=1","LocalVolumes":"0","Mounts":"","Names":"trusting_goldstine","Networks":"bridge","Platform":{"architecture":"arm64","os":"linux"},"Ports":"0.0.0.0:55000-\u003e8080/tcp","RunningFor":"3 hours ago","Size":"0B","State":"running","Status":"Up 3 hours"} Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 7aa6c79 commit 67c0be4

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

cli/command/formatter/container.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/docker/docker/api/types/container"
1717
"github.com/docker/docker/pkg/stringid"
1818
"github.com/docker/go-units"
19+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1920
)
2021

2122
const (
@@ -30,6 +31,15 @@ const (
3031
platformHeader = "PLATFORM"
3132
)
3233

34+
// Platform wraps a [ocispec.Platform] to implement the stringer interface.
35+
type Platform struct {
36+
ocispec.Platform
37+
}
38+
39+
func (p Platform) String() string {
40+
return platforms.FormatAll(p.Platform)
41+
}
42+
3343
// NewContainerFormat returns a Format for rendering using a Context
3444
func NewContainerFormat(source string, quiet bool, size bool) Format {
3545
switch source {
@@ -213,11 +223,12 @@ func (c *ContainerContext) RunningFor() string {
213223

214224
// Platform returns a human-readable representation of the container's
215225
// platform if it is available.
216-
func (c *ContainerContext) Platform() string {
217-
if c.c.ImageManifestDescriptor != nil && c.c.ImageManifestDescriptor.Platform != nil {
218-
return platforms.FormatAll(*c.c.ImageManifestDescriptor.Platform)
226+
func (c *ContainerContext) Platform() *Platform {
227+
p := c.c.ImageManifestDescriptor
228+
if p == nil || p.Platform == nil {
229+
return nil
219230
}
220-
return ""
231+
return &Platform{*p.Platform}
221232
}
222233

223234
// Ports returns a comma-separated string representing open ports of the container

cli/command/formatter/container_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
464464
"Mounts": "",
465465
"Names": "foobar_baz",
466466
"Networks": "",
467-
"Platform": "",
467+
"Platform": nil,
468468
"Ports": "",
469469
"RunningFor": "About a minute ago",
470470
"Size": "0B",
@@ -481,7 +481,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
481481
"Mounts": "",
482482
"Names": "foobar_bar",
483483
"Networks": "",
484-
"Platform": "linux/amd64",
484+
"Platform": map[string]any{"architecture": "amd64", "os": "linux"},
485485
"Ports": "",
486486
"RunningFor": "About a minute ago",
487487
"Size": "0B",
@@ -498,7 +498,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
498498
"Mounts": "",
499499
"Names": "foobar_bar",
500500
"Networks": "",
501-
"Platform": "unknown",
501+
"Platform": map[string]any{"architecture": "", "os": ""},
502502
"Ports": "",
503503
"RunningFor": "About a minute ago",
504504
"Size": "0B",

0 commit comments

Comments
 (0)