Skip to content

Commit edcd96b

Browse files
jsternbergthaJeztah
authored andcommitted
docker ps: add "Platform" as formatting option
docker ps --format 'table {{.ID}}\t{{.Image}}{{.Command}}\t{{.RunningFor}}\t{{.Status}}\t{{.Ports}}\t{{.Names}}\t{{.Platform}}' CONTAINER ID IMAGECOMMAND CREATED STATUS PORTS NAMES PLATFORM e422855eac55 docker-cli-dev"/bin/bash" 12 minutes ago Up 12 minutes strange_jennings linux/arm64 Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com> Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent c80675b commit edcd96b

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

cli/command/formatter/container.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"time"
1313

14+
"github.com/containerd/platforms"
1415
"github.com/distribution/reference"
1516
"github.com/docker/docker/api/types/container"
1617
"github.com/docker/docker/pkg/stringid"
@@ -26,6 +27,7 @@ const (
2627
mountsHeader = "MOUNTS"
2728
localVolumes = "LOCAL VOLUMES"
2829
networksHeader = "NETWORKS"
30+
platformHeader = "PLATFORM"
2931
)
3032

3133
// NewContainerFormat returns a Format for rendering using a Context
@@ -111,6 +113,7 @@ func NewContainerContext() *ContainerContext {
111113
"Mounts": mountsHeader,
112114
"LocalVolumes": localVolumes,
113115
"Networks": networksHeader,
116+
"Platform": platformHeader,
114117
}
115118
return &containerCtx
116119
}
@@ -210,6 +213,17 @@ func (c *ContainerContext) RunningFor() string {
210213
return units.HumanDuration(time.Now().UTC().Sub(createdAt)) + " ago"
211214
}
212215

216+
// Platform returns a human-readable representation of the platform
217+
// of the container if it is available.
218+
//
219+
// TODO(thaJeztah): should this take arguments to control "how" it's formatted?
220+
func (c *ContainerContext) Platform() string {
221+
if c.c.ImageManifestDescriptor != nil && c.c.ImageManifestDescriptor.Platform != nil {
222+
return platforms.FormatAll(*c.c.ImageManifestDescriptor.Platform)
223+
}
224+
return ""
225+
}
226+
213227
// Ports returns a comma-separated string representing open ports of the container
214228
// e.g. "0.0.0.0:80->9090/tcp, 9988/tcp"
215229
// it's used by command 'docker ps'

0 commit comments

Comments
 (0)