Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting docker stats programmatically

Tags:

python

docker

I am using docker-py and trying to get docker stats. But I am unable to get any API for returning stats for particular container. Is there a REST API or any other way to programmatically get the stats ?

>>> cli = docker.Client(base_url="tcp://xxxxx:2375", version='1.21')
>>> cli.containers() >> gives the right o/p
>>> cli.containers.get()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'get'

>>> docker.version
'1.10.6'
like image 648
Vin Avatar asked Mar 09 '23 13:03

Vin


2 Answers

You may be looking for Container.stats.

like image 191
shad0w_wa1k3r Avatar answered Mar 20 '23 01:03

shad0w_wa1k3r


  1. Get stats for a particular container name example: "monitoring-tinydb". Note: change this container name with your desired container id or name.

    import docker
    client = docker.from_env()
    container = client.containers.get("monitoring-tinydb")
    status = container.stats(decode=None, stream = False)
    print(status)
    
    
  2. Get stats for all containers running in the docker host

    import docker
    client = docker.DockerClient(base_url='unix:///var/run/docker.sock')
    for containers in client.containers.list():
        print(containers.stats(decode=None, stream = False))
    
like image 25
krishna kumar mishra Avatar answered Mar 20 '23 01:03

krishna kumar mishra