Skip to content

Commit eb3a7c2

Browse files
committed
Send "Name" and "ID" when stating stopped containers
When `docker stats` stopped containers, client will get empty stats data, this commit will gurantee client always get "Name" and "ID" field, so that it can format with `ID` and `Name` fields successfully. Signed-off-by: Zhang Wei <[email protected]>
1 parent 40dbbd3 commit eb3a7c2

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

daemon/stats.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ func (daemon *Daemon) ContainerStats(ctx context.Context, prefixOrName string, c
3333

3434
// If the container is either not running or restarting and requires no stream, return an empty stats.
3535
if (!container.IsRunning() || container.IsRestarting()) && !config.Stream {
36-
return json.NewEncoder(config.OutStream).Encode(&types.Stats{})
36+
return json.NewEncoder(config.OutStream).Encode(&types.StatsJSON{
37+
Name: container.Name,
38+
ID: container.ID})
3739
}
3840

3941
outStream := config.OutStream

daemon/stats/collector.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/Sirupsen/logrus"
9+
"github.com/docker/docker/api/types"
910
"github.com/docker/docker/container"
1011
"github.com/docker/docker/pkg/pubsub"
1112
)
@@ -84,7 +85,14 @@ func (s *Collector) Run() {
8485
if err != nil {
8586
if _, ok := err.(notRunningErr); !ok {
8687
logrus.Errorf("collecting stats for %s: %v", pair.container.ID, err)
88+
continue
8789
}
90+
91+
// publish empty stats containing only name and ID if not running
92+
pair.publisher.Publish(types.StatsJSON{
93+
Name: pair.container.Name,
94+
ID: pair.container.ID,
95+
})
8896
continue
8997
}
9098
// FIXME: move to containerd on Linux (not Windows)

integration-cli/docker_cli_stats_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,22 @@ func (s *DockerSuite) TestStatsAllNewContainersAdded(c *check.C) {
157157
// ignore, done
158158
}
159159
}
160+
161+
func (s *DockerSuite) TestStatsFormatAll(c *check.C) {
162+
// Windows does not support stats
163+
testRequires(c, DaemonIsLinux)
164+
165+
dockerCmd(c, "run", "-d", "--name=RunningOne", "busybox", "top")
166+
c.Assert(waitRun("RunningOne"), check.IsNil)
167+
dockerCmd(c, "run", "-d", "--name=ExitedOne", "busybox", "top")
168+
dockerCmd(c, "stop", "ExitedOne")
169+
c.Assert(waitExited("ExitedOne", 5*time.Second), check.IsNil)
170+
171+
out, _ := dockerCmd(c, "stats", "--no-stream", "--format", "{{.Name}}")
172+
c.Assert(out, checker.Contains, "RunningOne")
173+
c.Assert(out, checker.Not(checker.Contains), "ExitedOne")
174+
175+
out, _ = dockerCmd(c, "stats", "--all", "--no-stream", "--format", "{{.Name}}")
176+
c.Assert(out, checker.Contains, "RunningOne")
177+
c.Assert(out, checker.Contains, "ExitedOne")
178+
}

0 commit comments

Comments
 (0)