如何检查进程是否在docker容器内运行 [英] How to check if a process is running inside docker container

查看:2463
本文介绍了如何检查进程是否在docker容器内运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[Updated1]我有一个shell会改变一些功能中的TCP内核参数,但现在我需要使这个shell在Docker容器中运行,这意味着shell需要知道它正在一个容器内运行并停止配置内核。



现在我不知道如何实现,这里是 / proc / self / cgroup 在容器内:

  9:hugetlb:/ 
8:perf_event:/
7:blkio: /
6:冰柜:/
5:设备:/
4:内存:/
3:cpuacct:/
2:cpu:/ docker / 25ef774c390558ad8c4e9a859​​0b6a1956231aae404d6a7aba4dde320ff569b8b
1:cpuset:/

上面的任何标志可以用来弄清楚这个过程是运行在容器内?



[已更新2]:我也注意到了确定一个进程是否在lxc / Docker中运行,但在这种情况下似乎不起作用, / proc / 1 / cgroup中的内容我的容器的是:

  8:perf_event:/ 
7:blkio:/
6:冰柜:/
5:设备:/
4:内存:/
3:cpuacct:/
2:cpu:/ docker / 25ef774c390558ad8c4e9a859​​0b6a1956231aae404d6a7aba4 dde320ff569b8b
1:cpuset:/

No / lxc / containerid

如果您在Docker容器内,检查Docker容器是否可以通过 / proc / 1 / cgroup 。由于此帖建议您可以执行以下操作:



在docker容器之外, / proc / 1 / cgroup 中的所有条目都以 / 如下所示:

  vagrant @ ubuntu-13:〜$ cat / proc / 1 / cgroup 
11:name = systemd:/
10:hugetlb:/
9:perf_event:/
8:blkio:/
7:freezer:/
6:设备:/
5:内存:/
4:cpuacct:/
3:cpu:/
2:cpuset:/

在Docker容器内部,一些控制组将属于Docker(或LXC):


$ b $ ubuntu-13:〜$ docker运行busybox cat / proc / 1 / cgroup
11:name = systemd:/
10:hugetlb: /
9:perf_event:/
8:blkio:/
7:freezer:/
6:devices:/ docker / 3601745b3bd54d9780436faa5f0e4f72bb46231663bb99a6bb892764917832c2
5:memory:/
4:cpuacct:/
3:cpu:/ docker / 3601745b3bd54d9780436faa5f0e4f72bb462 31663bb99a6bb892764917832c2
2:cpuset:/


[Updated1] I have a shell which will change TCP kernel parameters in some functions, but now I need to make this shell run in Docker container, that means, the shell need to know it is running inside a container and stop configuring the kernel.

Now I'm not sure how to achieve that, here is the contents of /proc/self/cgroup inside the container:

9:hugetlb:/
8:perf_event:/
7:blkio:/
6:freezer:/
5:devices:/
4:memory:/
3:cpuacct:/
2:cpu:/docker/25ef774c390558ad8c4e9a8590b6a1956231aae404d6a7aba4dde320ff569b8b
1:cpuset:/

Any flags above can I use to figure out if this process is running inside a container?

[Updated2]: I have also noticed Determining if a process runs inside lxc/Docker, but it seems not working in this case, the content in /proc/1/cgroup of my container is:

8:perf_event:/
7:blkio:/
6:freezer:/
5:devices:/
4:memory:/
3:cpuacct:/
2:cpu:/docker/25ef774c390558ad8c4e9a8590b6a1956231aae404d6a7aba4dde320ff569b8b
1:cpuset:/

No /lxc/containerid

解决方案

To check inside a Docker container if you are inside a Docker container or not can be done via /proc/1/cgroup. As this post suggests you can to the following:

Outside a docker container all entries in /proc/1/cgroup end on / as you can see here:

vagrant@ubuntu-13:~$ cat /proc/1/cgroup
11:name=systemd:/
10:hugetlb:/
9:perf_event:/
8:blkio:/
7:freezer:/
6:devices:/
5:memory:/
4:cpuacct:/
3:cpu:/
2:cpuset:/

Inside a Docker container some of the control groups will belong to Docker (or LXC):

vagrant@ubuntu-13:~$ docker run busybox cat /proc/1/cgroup
11:name=systemd:/
10:hugetlb:/
9:perf_event:/
8:blkio:/
7:freezer:/
6:devices:/docker/3601745b3bd54d9780436faa5f0e4f72bb46231663bb99a6bb892764917832c2
5:memory:/
4:cpuacct:/
3:cpu:/docker/3601745b3bd54d9780436faa5f0e4f72bb46231663bb99a6bb892764917832c2
2:cpuset:/

这篇关于如何检查进程是否在docker容器内运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆