简体   繁体   中英

How to get container stats details using python docker

I am trying to get the docker container stats inside my python code after running the container as shown below. I am referring to python docker SDk https://docker-py.readthedocs.io/en/stable/index.html to run and get the details of container.

container = docker_client.containers.run(image = image,entrypoint = entrypoint,detach=True,tty=False,volumes = {FILE_PATH+input_file:{'bind': '/src/input.txt', 'mode': 'rw'}})
container.wait()
data = container.stats(stream=False)

The stats which are getting doesn't have memory and few other details properly. Below are the stats details

{
  'read': '0001-01-01T00:00:00Z',
  'preread': '2020-04-21T10:07:54.773647854Z',
  'pids_stats': {

  },
  'blkio_stats': {
    'io_service_bytes_recursive': None,
    'io_serviced_recursive': None,
    'io_queue_recursive': None,
    'io_service_time_recursive': None,
    'io_wait_time_recursive': None,
    'io_merged_recursive': None,
    'io_time_recursive': None,
    'sectors_recursive': None
  },
  'num_procs': 0,
  'storage_stats': {

  },
  'cpu_stats': {
    'cpu_usage': {
      'total_usage': 0,
      'usage_in_kernelmode': 0,
      'usage_in_usermode': 0
    },
    'throttling_data': {
      'periods': 0,
      'throttled_periods': 0,
      'throttled_time': 0
    }
  },
  'precpu_stats': {
    'cpu_usage': {
      'total_usage': 208804435,
      'percpu_usage': [
        2260663,
        0,
        0,
        7976886,
        0,
        2549616,
        178168661,
        1717192,
        117608,
        0,
        1011534,
        3305192,
        0,
        11372783,
        0,
        324300
      ],
      'usage_in_kernelmode': 20000000,
      'usage_in_usermode': 160000000
    },
    'system_cpu_usage': 98001601690000000,
    'online_cpus': 16,
    'throttling_data': {
      'periods': 0,
      'throttled_periods': 0,
      'throttled_time': 0
    }
  },
  'memory_stats': {

  },
  'name': '/quizzical_mcclintock',
  'id': '4bb79d8468f2f91a91022b4a7086744a6b3cdefab2a98f7efa178c9aff7ed246'
}

How to get all the stats details properly using python docker SDK?

import os
os.system("docker stats $(docker ps -q) --no-stream")

if you want to get the stats of all the running container then the following command will help you. Otherwise you need to calculate the stats yourself from total used so can be tricky.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM