mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
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 <zhangwei555@huawei.com>
This commit is contained in:
parent
40dbbd3f9b
commit
eb3a7c2329
3 changed files with 30 additions and 1 deletions
|
@ -33,7 +33,9 @@ func (daemon *Daemon) ContainerStats(ctx context.Context, prefixOrName string, c
|
||||||
|
|
||||||
// If the container is either not running or restarting and requires no stream, return an empty stats.
|
// If the container is either not running or restarting and requires no stream, return an empty stats.
|
||||||
if (!container.IsRunning() || container.IsRestarting()) && !config.Stream {
|
if (!container.IsRunning() || container.IsRestarting()) && !config.Stream {
|
||||||
return json.NewEncoder(config.OutStream).Encode(&types.Stats{})
|
return json.NewEncoder(config.OutStream).Encode(&types.StatsJSON{
|
||||||
|
Name: container.Name,
|
||||||
|
ID: container.ID})
|
||||||
}
|
}
|
||||||
|
|
||||||
outStream := config.OutStream
|
outStream := config.OutStream
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/container"
|
"github.com/docker/docker/container"
|
||||||
"github.com/docker/docker/pkg/pubsub"
|
"github.com/docker/docker/pkg/pubsub"
|
||||||
)
|
)
|
||||||
|
@ -84,7 +85,14 @@ func (s *Collector) Run() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if _, ok := err.(notRunningErr); !ok {
|
if _, ok := err.(notRunningErr); !ok {
|
||||||
logrus.Errorf("collecting stats for %s: %v", pair.container.ID, err)
|
logrus.Errorf("collecting stats for %s: %v", pair.container.ID, err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// publish empty stats containing only name and ID if not running
|
||||||
|
pair.publisher.Publish(types.StatsJSON{
|
||||||
|
Name: pair.container.Name,
|
||||||
|
ID: pair.container.ID,
|
||||||
|
})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// FIXME: move to containerd on Linux (not Windows)
|
// FIXME: move to containerd on Linux (not Windows)
|
||||||
|
|
|
@ -157,3 +157,22 @@ func (s *DockerSuite) TestStatsAllNewContainersAdded(c *check.C) {
|
||||||
// ignore, done
|
// ignore, done
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *DockerSuite) TestStatsFormatAll(c *check.C) {
|
||||||
|
// Windows does not support stats
|
||||||
|
testRequires(c, DaemonIsLinux)
|
||||||
|
|
||||||
|
dockerCmd(c, "run", "-d", "--name=RunningOne", "busybox", "top")
|
||||||
|
c.Assert(waitRun("RunningOne"), check.IsNil)
|
||||||
|
dockerCmd(c, "run", "-d", "--name=ExitedOne", "busybox", "top")
|
||||||
|
dockerCmd(c, "stop", "ExitedOne")
|
||||||
|
c.Assert(waitExited("ExitedOne", 5*time.Second), check.IsNil)
|
||||||
|
|
||||||
|
out, _ := dockerCmd(c, "stats", "--no-stream", "--format", "{{.Name}}")
|
||||||
|
c.Assert(out, checker.Contains, "RunningOne")
|
||||||
|
c.Assert(out, checker.Not(checker.Contains), "ExitedOne")
|
||||||
|
|
||||||
|
out, _ = dockerCmd(c, "stats", "--all", "--no-stream", "--format", "{{.Name}}")
|
||||||
|
c.Assert(out, checker.Contains, "RunningOne")
|
||||||
|
c.Assert(out, checker.Contains, "ExitedOne")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue