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>
This commit is contained in:
parent
af090512a6
commit
7aa6c79c0a
@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/containerd/platforms"
|
||||
"github.com/distribution/reference"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/pkg/stringid"
|
||||
@ -26,6 +27,7 @@ const (
|
||||
mountsHeader = "MOUNTS"
|
||||
localVolumes = "LOCAL VOLUMES"
|
||||
networksHeader = "NETWORKS"
|
||||
platformHeader = "PLATFORM"
|
||||
)
|
||||
|
||||
// NewContainerFormat returns a Format for rendering using a Context
|
||||
@ -109,6 +111,7 @@ func NewContainerContext() *ContainerContext {
|
||||
"Mounts": mountsHeader,
|
||||
"LocalVolumes": localVolumes,
|
||||
"Networks": networksHeader,
|
||||
"Platform": platformHeader,
|
||||
}
|
||||
return &containerCtx
|
||||
}
|
||||
@ -208,6 +211,15 @@ func (c *ContainerContext) RunningFor() string {
|
||||
return units.HumanDuration(time.Now().UTC().Sub(createdAt)) + " ago"
|
||||
}
|
||||
|
||||
// Platform returns a human-readable representation of the container's
|
||||
// platform if it is available.
|
||||
func (c *ContainerContext) Platform() string {
|
||||
if c.c.ImageManifestDescriptor != nil && c.c.ImageManifestDescriptor.Platform != nil {
|
||||
return platforms.FormatAll(*c.c.ImageManifestDescriptor.Platform)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Ports returns a comma-separated string representing open ports of the container
|
||||
// e.g. "0.0.0.0:80->9090/tcp, 9988/tcp"
|
||||
// it's used by command 'docker ps'
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
"github.com/docker/cli/internal/test"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/pkg/stringid"
|
||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"gotest.tools/v3/assert"
|
||||
is "gotest.tools/v3/assert/cmp"
|
||||
"gotest.tools/v3/golden"
|
||||
@ -425,13 +426,36 @@ func TestContainerContextWriteWithNoContainers(t *testing.T) {
|
||||
func TestContainerContextWriteJSON(t *testing.T) {
|
||||
unix := time.Now().Add(-65 * time.Second).Unix()
|
||||
containers := []container.Summary{
|
||||
{ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu", Created: unix, State: container.StateRunning},
|
||||
{ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unix, State: container.StateRunning},
|
||||
{
|
||||
ID: "containerID1",
|
||||
Names: []string{"/foobar_baz"},
|
||||
Image: "ubuntu",
|
||||
Created: unix,
|
||||
State: container.StateRunning,
|
||||
},
|
||||
{
|
||||
ID: "containerID2",
|
||||
Names: []string{"/foobar_bar"},
|
||||
Image: "ubuntu",
|
||||
Created: unix,
|
||||
State: container.StateRunning,
|
||||
|
||||
ImageManifestDescriptor: &ocispec.Descriptor{Platform: &ocispec.Platform{Architecture: "amd64", OS: "linux"}},
|
||||
},
|
||||
{
|
||||
ID: "containerID3",
|
||||
Names: []string{"/foobar_bar"},
|
||||
Image: "ubuntu",
|
||||
Created: unix,
|
||||
State: container.StateRunning,
|
||||
|
||||
ImageManifestDescriptor: &ocispec.Descriptor{Platform: &ocispec.Platform{}},
|
||||
},
|
||||
}
|
||||
expectedCreated := time.Unix(unix, 0).String()
|
||||
expectedJSONs := []map[string]any{
|
||||
{
|
||||
"Command": "\"\"",
|
||||
"Command": `""`,
|
||||
"CreatedAt": expectedCreated,
|
||||
"ID": "containerID1",
|
||||
"Image": "ubuntu",
|
||||
@ -440,6 +464,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
|
||||
"Mounts": "",
|
||||
"Names": "foobar_baz",
|
||||
"Networks": "",
|
||||
"Platform": "",
|
||||
"Ports": "",
|
||||
"RunningFor": "About a minute ago",
|
||||
"Size": "0B",
|
||||
@ -447,7 +472,7 @@ func TestContainerContextWriteJSON(t *testing.T) {
|
||||
"Status": "",
|
||||
},
|
||||
{
|
||||
"Command": "\"\"",
|
||||
"Command": `""`,
|
||||
"CreatedAt": expectedCreated,
|
||||
"ID": "containerID2",
|
||||
"Image": "ubuntu",
|
||||
@ -456,6 +481,24 @@ func TestContainerContextWriteJSON(t *testing.T) {
|
||||
"Mounts": "",
|
||||
"Names": "foobar_bar",
|
||||
"Networks": "",
|
||||
"Platform": "linux/amd64",
|
||||
"Ports": "",
|
||||
"RunningFor": "About a minute ago",
|
||||
"Size": "0B",
|
||||
"State": "running",
|
||||
"Status": "",
|
||||
},
|
||||
{
|
||||
"Command": `""`,
|
||||
"CreatedAt": expectedCreated,
|
||||
"ID": "containerID3",
|
||||
"Image": "ubuntu",
|
||||
"Labels": "",
|
||||
"LocalVolumes": "0",
|
||||
"Mounts": "",
|
||||
"Names": "foobar_bar",
|
||||
"Networks": "",
|
||||
"Platform": "unknown",
|
||||
"Ports": "",
|
||||
"RunningFor": "About a minute ago",
|
||||
"Size": "0B",
|
||||
|
Loading…
x
Reference in New Issue
Block a user