概述

虽然 Docker 的资源占用很多时候可以通过宿主机来看,但是,这不够直观,所以这里我就介绍一种查看 Docker 各个容器资源占用的方式。

docker stats

Docker 其实原生就提供了资源查看命令 docker stats

  1. [[email protected].io]# docker stats
  2. CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
  3. 5d6a756e3e5c admiring_austin 0.51% 25.15MiB / 14.96GiB 0.16% 3.26MB / 0B 118MB / 74.5MB 27
  4. 34a623e66969 deployments_lauxxxx_1 0.07% 5.191MiB / 14.96GiB 0.03% 22.7MB / 14MB 58.9MB / 120kB 4
  5. a6863e4e6063 deployments_mongo_1 0.47% 21.87MiB / 14.96GiB 0.14% 18.5MB / 19.4MB 141MB / 97.5MB 29
  6. 504825258e06 postgres 0.03% 1.816MiB / 14.96GiB 0.01% 4.64MB / 454kB 233MB / 118MB 7
  7. 95f212e7a748 mysql 0.16% 640KiB / 14.96GiB 0.00% 5.49MB / 1.7MB 148MB / 121MB 30

如果想看某一个容器的:

  1. [[email protected].io]# docker stats a6863e4e6063
  2. CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
  3. a6863e4e6063 deployments_mongo_1 0.82% 21.87MiB / 14.96GiB 0.14% 18.5MB / 19.4MB 141MB / 97.5MB 29

查看磁盘使用量

  1. [[email protected].io]# docker system df
  2. TYPE TOTAL ACTIVE SIZE RECLAIMABLE
  3. Images 104 17 9.701GB 7.821GB (80%)
  4. Containers 24 2 18.29MB 17.55MB (95%)
  5. Local Volumes 47 5 316.2MB 1.093kB (0%)
  6. Build Cache 0 0 0B 0B

查看每个容器硬盘使用量

  1. [[email protected].io]# docker ps --size
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
  3. 1b1a7d8bb63b xxxxx "gunicorn --bind 0.0…" 11 days ago Up 3 minutes 0.0.0.0:5000->5000/tcp deployments_lauxxxx_1 740kB (virtual 1.05GB)
  4. d796d2b52f69 mongo:4.1 "docker-entrypoint.s…" 11 days ago Up 3 minutes 0.0.0.0:27017->27017/tcp deployments_mongo_1 281B (virtual 360MB)

开源实现

如果你觉得原生的太原始,想看炫酷点的,不妨试一下:ctop

图 1:ctop 截图

Ref