Nitroethane
2021-04-19 12:18:13 +08:00
对于没有在 docker 中的 PID 为 1 的进程来说,它的 cgroup 目录都是 /。而对于 docker 中的进程来说,它的 cgroup 目录会包含 /docker 子字符串。下面是我的 Linux 上的结果。
docker 中 PID 为 1 的进程:
$ cat /proc/1/cgroup
12:hugetlb:/system.slice/docker-e0539beecb2db33df4af004f03877dda0ae7b880775bf1d7cf36e62013c468bf.scope
11:cpuset:/system.slice/docker-e0539beecb2db33df4af004f03877dda0ae7b880775bf1d7cf36e62013c468bf.scope
10:perf_event:/system.slice/docker-e0539beecb2db33df4af004f03877dda0ae7b880775bf1d7cf36e62013c468bf.scope
9:freezer:/system.slice/docker-e0539beecb2db33df4af004f03877dda0ae7b880775bf1d7cf36e62013c468bf.scope
8:memory:/system.slice/docker-e0539beecb2db33df4af004f03877dda0ae7b880775bf1d7cf36e62013c468bf.scope
7:rdma:/
6:blkio:/system.slice/docker-e0539beecb2db33df4af004f03877dda0ae7b880775bf1d7cf36e62013c468bf.scope
5:pids:/system.slice/docker-e0539beecb2db33df4af004f03877dda0ae7b880775bf1d7cf36e62013c468bf.scope
4:net_cls,net_prio:/system.slice/docker-e0539beecb2db33df4af004f03877dda0ae7b880775bf1d7cf36e62013c468bf.scope
3:cpu,cpuacct:/system.slice/docker-e0539beecb2db33df4af004f03877dda0ae7b880775bf1d7cf36e62013c468bf.scope
2:devices:/system.slice/docker-e0539beecb2db33df4af004f03877dda0ae7b880775bf1d7cf36e62013c468bf.scope
1:name=systemd:/system.slice/docker-e0539beecb2db33df4af004f03877dda0ae7b880775bf1d7cf36e62013c468bf.scope
0::/system.slice/docker-e0539beecb2db33df4af004f03877dda0ae7b880775bf1d7cf36e62013c468bf.scope
宿主机中 PID 为 1 的进程:
$ cat /proc/1/cgroup
12:hugetlb:/
11:cpuset:/
10:perf_event:/
9:freezer:/
8:memory:/init.scope
7:rdma:/
6:blkio:/init.scope
5:pids:/init.scope
4:net_cls,net_prio:/
3:cpu,cpuacct:/init.scope
2:devices:/init.scope
1:name=systemd:/init.scope
0::/init.scope
还可通过检测是否存在 /.dockerenv 文件来判断。