查看容器内存、CPU使用情况,可以是可以,但是还是不够详细, 可能大多数人在这一步就OK了,也没必要往下看了)

    1. docker stats --no-stream

    相关命令
    根据内存占用大小排序

    1. docker stats --no-stream | sort -rk7

    我需要知道docker容器自己使用了多少cache memory以及更多信息。
    经过各种Google,找到这篇文章 Getting memory usage in Linux and Docker,他也提到在容器里使用 free -h 和在宿主机使用 free -h 是一样的,free -h 本质上是读取 /proc/meminfo 文件内容。

    文章中简单的介绍了下Docker背后隔离原理:

    According to Docker Overview: The Underlying Technology, processes in a Docker container directly run in their host OS without any virtualization, but they are isolated from the host OS and other containers in effect thanks to these Linux kernel features:

    namespaces: Isolate PIDs, hostnames, user IDs, network accesses, IPC, etc. cgroups: Limit resource usage UnionFS: Isolate file system Because of the namespaces, ps command lists processes of Docker containers in addition to other processes in the host OS, while it cannot list processes of host OS or other containers in a docker container.

    By default, Docker containers have no resource constraints. So, if you run one container in a host and don’t limit resource usage of the container, and this is my case, the container’s “free memory” is same as the host OS’s “free memory”.

    核心意思是 资源不隔离。所以 the container’s “free memory” is same as the host OS’s “free memory”.
    然而要怎么样才能获取到容器本身的资源信息呢?
    进入容器的 /sys/fs/cgroup/memory/ 这个目录

    里面有关内存的一些信息。如下:

    1. [root@call-1299-051551186918-880000000248 /]# cd /sys/fs/cgroup/memory/
    2. [root@call-1299-051551186918-880000000248 memory]# ls
    3. cgroup.clone_children memory.kmem.failcnt memory.kmem.tcp.limit_in_bytes memory.max_usage_in_bytes memory.move_charge_at_immigrate memory.stat tasks
    4. cgroup.event_control memory.kmem.limit_in_bytes memory.kmem.tcp.max_usage_in_bytes memory.memsw.failcnt memory.numa_stat memory.swappiness
    5. cgroup.procs memory.kmem.max_usage_in_bytes memory.kmem.tcp.usage_in_bytes memory.memsw.limit_in_bytes memory.oom_control memory.usage_in_bytes
    6. memory.failcnt memory.kmem.slabinfo memory.kmem.usage_in_bytes memory.memsw.max_usage_in_bytes memory.pressure_level memory.use_hierarchy
    7. memory.force_empty memory.kmem.tcp.failcnt memory.limit_in_bytes memory.memsw.usage_in_bytes memory.soft_limit_in_bytes notify_on_release
    8. [root@call-1299-051551186918-880000000248 memory]#

    查看 memory.stat 文件内容,可以得到很多信息,包括我需要的cache memory:

    1. [root@call-1299-051551186918-880000000248 memory]# cat memory.stat
    2. cache 4157440
    3. rss 2686976
    4. rss_huge 0
    5. shmem 0
    6. mapped_file 4595712
    7. dirty 0
    8. writeback 0
    9. swap 0
    10. pgpgin 356400
    11. pgpgout 359942
    12. pgfault 802032
    13. pgmajfault 0
    14. inactive_anon 0
    15. active_anon 3289088
    16. inactive_file 1146880
    17. active_file 786432
    18. unevictable 0
    19. hierarchical_memory_limit 9223372036854771712
    20. hierarchical_memsw_limit 9223372036854771712
    21. total_cache 4157440
    22. total_rss 2686976
    23. total_rss_huge 0
    24. total_shmem 0
    25. total_mapped_file 4595712
    26. total_dirty 0
    27. total_writeback 0
    28. total_swap 0
    29. total_pgpgin 356400
    30. total_pgpgout 359942
    31. total_pgfault 802032
    32. total_pgmajfault 0
    33. total_inactive_anon 0
    34. total_active_anon 3289088
    35. total_inactive_file 1146880
    36. total_active_file 786432
    37. total_unevictable 0
    38. [root@call-1299-051551186918-880000000248 memory]#
    • total_cache 1579098112 : 缓存内存
    • total_rss 497885184:已使用内存

    附注:memory.stat 每项含义请看这篇文章https://xuxinkun.github.io/2016/05/16/memory-monitor-with-cgroup/