Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Container stats stream returns data after container is stopped #3291

Open
franzwong opened this issue Sep 29, 2024 · 0 comments
Open

Container stats stream returns data after container is stopped #3291

franzwong opened this issue Sep 29, 2024 · 0 comments

Comments

@franzwong
Copy link

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) ✗ 
@github-staff github-staff deleted a comment Sep 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant