Skip to content

Commit 5fb5e0b

Browse files
committed
docker stats --all: remove containers when removed
Before this patch, running `docker stats --all` would continue showing all containers once observed. For example; CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS f2a785b0cd5f foo1 0.00% 8.535MiB / 7.653GiB 0.11% 1.12kB / 126B 0B / 12.3kB 11 fc191b27517f foo2 0.00% 8.531MiB / 7.653GiB 0.11% 998B / 126B 0B / 12.3kB 11 5040185fba53 foo3 0.00% 8.578MiB / 7.653GiB 0.11% 872B / 126B 0B / 8.19kB 11 WHen removing `foo2`, the container would continue to be listed: CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS f2a785b0cd5f foo1 0.00% 8.535MiB / 7.653GiB 0.11% 1.12kB / 126B 0B / 12.3kB 11 fc191b27517f foo2 -- -- / -- -- -- -- -- 5040185fba53 foo3 0.00% 8.578MiB / 7.653GiB 0.11% 872B / 126B 0B / 12.3kB 11 Starting a new `foo2` container would now produce multiple entries: CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS f2a785b0cd5f foo1 0.00% 8.535MiB / 7.653GiB 0.11% 1.25kB / 126B 0B / 12.3kB 11 fc191b27517f foo2 -- -- / -- -- -- -- -- 5040185fba53 foo3 0.00% 8.578MiB / 7.653GiB 0.11% 998B / 126B 0B / 12.3kB 11 dba11b9e1ba9 foo2 0.00% 8.578MiB / 7.653GiB 0.11% 872B / 126B 0B / 8.19kB 11 Repeat that, and the list would continue to grow; CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS f2a785b0cd5f foo1 0.00% 8.535MiB / 7.653GiB 0.11% 1.25kB / 126B 0B / 12.3kB 11 fc191b27517f foo2 -- -- / -- -- -- -- -- 5040185fba53 foo3 0.00% 8.578MiB / 7.653GiB 0.11% 998B / 126B 0B / 12.3kB 11 dba11b9e1ba9 foo2 -- -- / -- -- -- -- -- 193a6dcfaa2d foo2 -- -- / -- -- -- -- -- bf50e58085c6 foo2 0.00% 8.539MiB / 7.653GiB 0.11% 872B / 126B 0B / 8.19kB 11 After this patch, containers are removed when we observe a `destroy` event; CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS f2a785b0cd5f foo1 0.00% 8.535MiB / 7.653GiB 0.11% 1.5kB / 126B 0B / 12.3kB 11 5040185fba53 foo3 0.00% 8.578MiB / 7.653GiB 0.11% 1.25kB / 126B 0B / 12.3kB 11 bf50e58085c6 foo2 0.00% 8.539MiB / 7.653GiB 0.11% 872B / 126B 0B / 12.3kB 11 Containers are added when created, so in the example above, the new `foo2` is added at the end: CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS f2a785b0cd5f foo1 0.00% 8.535MiB / 7.653GiB 0.11% 1.5kB / 126B 0B / 12.3kB 11 5040185fba53 foo3 0.00% 8.578MiB / 7.653GiB 0.11% 1.25kB / 126B 0B / 12.3kB 11 bf50e58085c6 foo2 0.00% 8.539MiB / 7.653GiB 0.11% 872B / 126B 0B / 12.3kB 11 If a container dies, and `--all` is set, we continue listing it, but stats are not updated while the container is stopped (we should consider resetting the stats and show `-- / --` to be more clear that we don't have the container running). Here's with `foo3` stopped: CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS f2a785b0cd5f foo1 0.00% 8.535MiB / 7.653GiB 0.11% 1.5kB / 126B 0B / 12.3kB 11 5040185fba53 foo3 0.00% 0B / 0B 0.00% 0B / 0B 0B / 0B 0 bf50e58085c6 foo2 0.00% 8.539MiB / 7.653GiB 0.11% 872B / 126B 0B / 12.3kB 11 Starting the container continues updating its stats: CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS f2a785b0cd5f foo1 0.00% 8.535MiB / 7.653GiB 0.11% 1.63kB / 126B 0B / 12.3kB 11 5040185fba53 foo3 0.00% 8.496MiB / 7.653GiB 0.11% 872B / 126B 0B / 0B 11 bf50e58085c6 foo2 0.00% 8.539MiB / 7.653GiB 0.11% 998B / 126B 0B / 12.3kB 11 When running without `--all`, we continue to remove containers as soon as possible (`die` events), but with `--all`, those events are ignored with the expectation that the container might come back. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 743c385 commit 5fb5e0b

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

cli/command/container/stats.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,20 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
156156
}
157157
})
158158

159+
// Remove containers when they are removed ("destroyed"); containers
160+
// do not emit [events.ActionRemove], only [events.ActionDestroy].
161+
//
162+
// When running with "--all" we don't remove containers when they die,
163+
// because they may come back, but without "--all" we remove them
164+
// on the first possible occasion (either "die" or "destroy").
165+
rmEvents := []events.Action{events.ActionDestroy}
159166
if !options.All {
160-
eh.setHandler([]events.Action{events.ActionDie}, func(ctx context.Context, e events.Message) {
161-
log.G(ctx).Debug("stop collecting stats for container")
162-
cStats.remove(e.Actor.ID)
163-
})
167+
rmEvents = append(rmEvents, events.ActionDie)
164168
}
169+
eh.setHandler(rmEvents, func(ctx context.Context, e events.Message) {
170+
log.G(ctx).Debug("stop collecting stats for container")
171+
cStats.remove(e.Actor.ID)
172+
})
165173

166174
// monitorContainerEvents watches for container creation and removal (only
167175
// used when calling `docker stats` without arguments).

0 commit comments

Comments
 (0)