Skip to content

Commit 7922984

Browse files
authored
Merge pull request #6863 from thaJeztah/fix_stats
fix: docker stats --all: remove containers when removed
2 parents bcfb717 + 5fb5e0b commit 7922984

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)