Container stats stream returns data after container is stopped #3291
Open
Description
I have tested the library with the following code. There was only 1 container before I stopped it at 11:49:53. After it stopped, the stream still returned data and the values were different. I use next
to retrieve the stats from the stream and I don't know if that is the proper way. Thanks.
def main():
client = docker.from_env()
containers = client.containers.list()
if len(containers) == 0:
print("There is no container")
return
stat_stream = containers[0].stats(decode=True, stream=True)
while True:
stat = next(stat_stream)
if "memory_stats" in stat:
name = stat["name"][1:]
usage = stat["memory_stats"]["usage"]
logger.info(f"{name}: {usage}")
time.sleep(5)
2024-09-29 11:49:44 __main__ INFO ubuntu-test: 4943872
2024-09-29 11:49:49 __main__ INFO ubuntu-test: 5328896
2024-09-29 11:49:54 __main__ INFO ubuntu-test: 5140480
2024-09-29 11:49:59 __main__ INFO ubuntu-test: 4956160
2024-09-29 11:50:04 __main__ INFO ubuntu-test: 5115904
2024-09-29 11:50:09 __main__ INFO ubuntu-test: 5074944
➜ ubuntu git:(main) ✗ docker stop ubuntu-test && date
ubuntu-test
Sun Sep 29 11:49:53 JST 2024
➜ ubuntu git:(main) ✗ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
➜ ubuntu git:(main) ✗
Metadata
Assignees
Labels
No labels