docker 查看容器CPU 以及memory实际使用情况

7 篇文章 0 订阅
docker stats
docker stats --no-stream

By default, docker stats will only output results for running containers. If you would like to output stats for all containers you can use the -a or --all flags with the command.

docker stats --no-stream -a
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Python 的 docker 模块来统计单个 Docker 容器从开始到结束的资源使用情况。下面是一个使用 docker 模块统计 Docker 容器资源使用情况的示例代码: ``` python import docker # 创建 Docker 客户端 client = docker.from_env() # 获取容器对象 container = client.containers.get('容器ID') # 获取容器CPU 使用情况 cpu_stats = container.stats(stream=False)['cpu_stats'] cpu_usage = cpu_stats['cpu_usage']['total_usage'] cpu_system = cpu_stats['system_cpu_usage'] cpu_percent = (cpu_usage / cpu_system) * 100 # 获取容器的内存使用情况 mem_stats = container.stats(stream=False)['memory_stats'] mem_usage = mem_stats['usage'] mem_limit = mem_stats['limit'] # 获取容器的网络使用情况 net_stats = container.stats(stream=False)['networks'] net_rx_bytes = sum([stats['rx_bytes'] for _, stats in net_stats.items()]) net_tx_bytes = sum([stats['tx_bytes'] for _, stats in net_stats.items()]) # 获取容器的 I/O 使用情况 io_stats = container.stats(stream=False)['blkio_stats'] io_read_bytes = sum([stats['value'] for stats in io_stats['io_service_bytes_recursive'] if stats['op'] == 'Read']) io_write_bytes = sum([stats['value'] for stats in io_stats['io_service_bytes_recursive'] if stats['op'] == 'Write']) # 输出容器的资源使用情况 print("CPU Usage: {:.2f}%".format(cpu_percent)) print("Memory Usage: {:.2f} MB / {:.2f} MB".format(mem_usage / 1024 / 1024, mem_limit / 1024 / 1024)) print("Network Usage: RX {:.2f} MB / TX {:.2f} MB".format(net_rx_bytes / 1024 / 1024, net_tx_bytes / 1024 / 1024)) print("I/O Usage: Read {:.2f} MB / Write {:.2f} MB".format(io_read_bytes / 1024 / 1024, io_write_bytes / 1024 / 1024)) ``` 注意,上述代码中获取容器资源使用情况的方法可能因为不同版本的 Docker API 或 docker 模块的版本而有所变化,具体可以参考官方文档进行调整。另外,上述代码中的单位为 MB,可以根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值