合 如何判断某个进程是否在docker容器中运行
如何判断进程是在docker内进程还是宿主机进程
直接通过命令cat /proc/进程号/cgroup
来判断,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | [root@lhrdb prometheusAlert]# ps -ef|grep Alert root 3436219 2662512 0 12:35 pts/0 00:00:00 ./PrometheusAlert root 3440224 3437868 1 12:36 pts/2 00:00:00 /usr/local/prometheusAlert/PrometheusAlert root 3441004 2662512 0 12:36 pts/0 00:00:00 grep --color=auto Alert [root@lhrdb prometheusAlert]# cat /proc/3436219/cgroup 11:perf_event:/ 10:hugetlb:/ 9:pids:/system.slice/sshd.service 8:memory:/system.slice/sshd.service 7:freezer:/ 6:devices:/system.slice/sshd.service 5:net_prio,net_cls:/ 4:cpuacct,cpu:/system.slice/sshd.service 3:cpuset:/ 2:blkio:/system.slice/sshd.service 1:name=systemd:/system.slice/sshd.service [root@lhrdb prometheusAlert]# cat /proc/3440224/cgroup 11:perf_event:/docker/3dc513293f0638c21cdb1f082f3cc75e71ecfa1fd27fc8405d00bfec6ee91956 10:hugetlb:/docker/3dc513293f0638c21cdb1f082f3cc75e71ecfa1fd27fc8405d00bfec6ee91956 9:pids:/docker/3dc513293f0638c21cdb1f082f3cc75e71ecfa1fd27fc8405d00bfec6ee91956 8:memory:/docker/3dc513293f0638c21cdb1f082f3cc75e71ecfa1fd27fc8405d00bfec6ee91956 7:freezer:/docker/3dc513293f0638c21cdb1f082f3cc75e71ecfa1fd27fc8405d00bfec6ee91956 6:devices:/docker/3dc513293f0638c21cdb1f082f3cc75e71ecfa1fd27fc8405d00bfec6ee91956 5:net_prio,net_cls:/docker/3dc513293f0638c21cdb1f082f3cc75e71ecfa1fd27fc8405d00bfec6ee91956 4:cpuacct,cpu:/docker/3dc513293f0638c21cdb1f082f3cc75e71ecfa1fd27fc8405d00bfec6ee91956 3:cpuset:/docker/3dc513293f0638c21cdb1f082f3cc75e71ecfa1fd27fc8405d00bfec6ee91956 2:blkio:/docker/3dc513293f0638c21cdb1f082f3cc75e71ecfa1fd27fc8405d00bfec6ee91956 1:name=systemd:/docker/3dc513293f0638c21cdb1f082f3cc75e71ecfa1fd27fc8405d00bfec6ee91956 [root@lhrdb ~]# docker ps | grep 3dc51329 3dc513293f06 lhrbest/lhrcentos76:9.0 "/usr/sbin/init" 2 weeks ago Up 29 minutes 0.0.0.0:322->22/tcp, :::322->22/tcp, 0.0.0.0:33000->3000/tcp, :::33000->3000/tcp, 0.0.0.0:33339->3389/tcp, :::33339->3389/tcp, 0.0.0.0:38080->8080/tcp, :::38080->8080/tcp, 0.0.0.0:39090->9090/tcp, :::39090->9090/tcp, 0.0.0.0:39093->9093/tcp, :::39093->9093/tcp, 0.0.0.0:39297->9297/tcp, :::39297->9297/tcp lhrsk_Promethues [root@lhrdb ~]# |