如何判断系统是不是处在docker中
时间: 2024-05-09 16:16:20 浏览: 86
可以通过运行命令 `cat /proc/1/cgroup` 查看当前进程所在的 cgroup 路径,如果路径中包含 `/docker` 关键词,则说明该进程运行在 Docker 容器中。例如:
```
$ cat /proc/1/cgroup
11:blkio:/docker/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
10:memory:/docker/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
9:hugetlb:/docker/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
8:cpuset:/docker/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
7:perf_event:/docker/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
6:net_prio:/docker/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
5:net_cls:/docker/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
4:freezer:/docker/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
3:devices:/docker/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
2:cpu,cpuacct:/docker/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
1:name=systemd:/docker/1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef
```
在上面的输出中,可以看到 `/docker` 出现在每个 cgroup 路径中,因此可以判断系统当前运行在 Docker 容器中。
阅读全文