From b5efd66ba6336080a10fcb9e7cec2e2704edf7ae Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 19 Mar 2026 23:36:32 +0100 Subject: [PATCH 1/2] cli/command/container: stats: make stripping "/" prefix deterministic This code was assuming the API always returns container names with a "/" prefix. While this is currently correct, we may at some point stop doing so. This patch changes the code to only trim "/" as prefix and not any other character. Signed-off-by: Sebastiaan van Stijn --- cli/command/container/formatter_stats.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cli/command/container/formatter_stats.go b/cli/command/container/formatter_stats.go index 626c63142d91..a7792f2bc89f 100644 --- a/cli/command/container/formatter_stats.go +++ b/cli/command/container/formatter_stats.go @@ -2,6 +2,7 @@ package container import ( "strconv" + "strings" "sync" "github.com/docker/cli/cli/command/formatter" @@ -167,9 +168,9 @@ func (c *statsContext) Container() string { } func (c *statsContext) Name() string { - // TODO(thaJeztah): make this explicitly trim the "/" prefix, not just any char. - if len(c.s.Name) > 1 { - return c.s.Name[1:] + // Trim the "/" prefix (if present). + if name := strings.TrimPrefix(c.s.Name, "/"); name != "" { + return name } return noValue } From dfda342e51c134135b25e70c7893998fe0451fcc Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 19 Mar 2026 23:45:37 +0100 Subject: [PATCH 2/2] cli/command/formatter: StripNamePrefix only strip "/" prefix This code was assuming the API always returns container names with a "/" prefix. While this is currently correct, we may at some point stop doing so. This patch changes the code to only trim "/" as prefix and not any other character. Signed-off-by: Sebastiaan van Stijn --- cli/command/formatter/container.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cli/command/formatter/container.go b/cli/command/formatter/container.go index fafa6a56e0d9..be1be92b4e7f 100644 --- a/cli/command/formatter/container.go +++ b/cli/command/formatter/container.go @@ -155,11 +155,12 @@ func (c *ContainerContext) Names() string { return strings.Join(names, ",") } -// StripNamePrefix removes prefix from string, typically container names as returned by `ContainersList` API +// StripNamePrefix removes any "/" prefix from container names returned +// by the "ContainersList" API. func StripNamePrefix(ss []string) []string { sss := make([]string, len(ss)) for i, s := range ss { - sss[i] = s[1:] + sss[i] = strings.TrimPrefix(s, "/") } return sss }