docker / docker/docker-py

Container stats stream returns data after container is stopped

Open
#3,291 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.2k
Forks
1.7k
Avg merge
13d 8h
Merged PRs (30d)
2

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) ✗ 

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test entry point is named. Start by reproducing the provided Python stats-stream example while stopping the container, then inspect the Docker Engine API and library handling of the stream. Done means the behavior is explained and a regression test covers the expected result after the container stops.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.