【问题标题】:How to get container stats details using python docker如何使用 python docker 获取容器统计信息
【发布时间】:2020-04-21 12:29:04
【问题描述】:

在运行容器后,我试图在我的 python 代码中获取 docker 容器统计信息,如下所示。我是指 python docker SDk https://docker-py.readthedocs.io/en/stable/index.html 运行并获取容器的详细信息。

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)

得到的统计数据没有正确的记忆和其他一些细节。以下是统计详情

{
  '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'
}

如何使用 python docker SDK 正确获取所有统计信息?

【问题讨论】:

    标签: python docker dockerpy docker-api python-docker


    【解决方案1】:
    import os
    os.system("docker stats $(docker ps -q) --no-stream")
    

    如果您想获取所有正在运行的容器的统计信息,那么以下命令将帮助您。否则,您需要自己根据使用总量计算统计数据,因此可能会很棘手。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 2011-03-07
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      相关资源
      最近更新 更多