question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

docker-py running container blocks access to "docker stats"

See original GitHub issue

I am using docker-py to run a short-running (1m-2h) container in the background and I’d like to monitor that container’s stats to record and characterize its I/O, CPU, and memory usage every second or two. But it appears that while containers are running under docker-py I can’t get stats, either from my code through docker-py (c.stats()) or the command line docker stats. Streaming or no streaming, both seem to just hang until they timeout, or the container just happens to finish and unblock docker.

I should note that I can call other functions on the docker-py Client, such as containers(). Just that the stats() call with no streaming or attempting to iterate over the generator with streaming just hangs until the container exits. Similar story with the command line. docker ps works, docker stats hangs.

I’m using Ubuntu 14.04, docker-py version 1.9.0, and docker version 1.12.1, plain vanilla from the repos, connecting over a local domain socket as configured by default (/var/run/docker.sock).

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:19 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
TomasTomecekcommented, Sep 9, 2016

So this is actually fixed in upstream: https://github.com/docker/docker/pull/25905

The problem is that container has disabled networking (SandboxID is set to "") and stat collector fails to pick networking stats (obviously) and won’t publish any stats to channel, meaning server will never respond with any stats.

To workaround this issue: enable networking. To fix this issue: wait for upstream to release a new version of docker (> 1.12.1).

2reactions
bartgranthamcommented, Sep 9, 2016

Wait! {'network_disabled' : False} did fix my problem! But it was masked by another important problem that might be related the source of the bug. In my code I iterate until the container disappears from the list of running containers:

while container['Id'] in [i['Id'] for i in c.containers()]:
        print c.stats(container['Id'], stream=False)
        time.sleep(2)

With {'network_disabled' : False} that loops correctly until the container is no longer running. Then, and I suspect there’s a race condition here, what should be the final call to stats() hangs. My solution is to set the client timeout to something smaller and less annoying like 15 seconds and do this:

    while container['Id'] in [i['Id'] for i in c.containers()]:
        try:
            stats = c.stats(container['Id'], stream=False)
        except:
            break
        print stats
        time.sleep(2)
Read more comments on GitHub >

github_iconTop Results From Across the Web

docker stats - Docker Documentation
The docker stats command returns a live data stream for running containers. To limit data to one or more specific containers, specify a...
Read more >
Analyzing Docker Container Performance With Native Tools
The docker stats command displays a live data stream with CPU, memory usage, memory limit, block I/O, and network IO metrics for all...
Read more >
How to Collect Docker Metrics - Datadog
Access via, CPU metrics, Memory metrics, I/O metrics, Network metrics ... cat /sys/fs/cgroup/cpuacct/docker/$CONTAINER_ID/cpuacct.stat ...
Read more >
Understanding “docker stats” Command Output
It is the percentage of the host's CPU and memory the container is using. If the host is using memory for other processes,...
Read more >
Understand how to monitor Docker Metrics with docker stats
Docker containers are transient (lasting for a very short time), spawning quickly and in high numbers, which causes metrics bursts.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found