转载 https://www.cnblogs.com/keithtt/p/7591097.html
找到指定容器的所有进程的PID可以更方便的对容器进程进行管理,特别是在某些容器卡住无法连接的场景。
1、找出容器ID
# docker ps
2、进入相应目录
# cd /sys/fs/cgroup/memory/docker/d14e1a6182eeed7c8f2a7c0a315a790a16bfbab1fdc7a73813cdeee494e8050a/
3、进程号就存在一个文件里面
# cat cgroup.procs
1761
1869
1877
1892
1900
1903
3512
4793
4794
4、查看所有进程,找出对应的进程树分支
# ps auxwwf
...
root 1761 0.0 0.0 11360 2432 pts/13 Ss 06:40 0:00 \_ /bin/bash /root/bin/autoboot.sh
root 1869 0.0 0.0 66744 2772 ? Ss 06:40 0:00 \_ /usr/sbin/sshd
497 1877 0.0 0.0 41396 2732 ? Ss 06:40 0:00 \_ /usr/sbin/nrpe -c /etc/nagios/nrpe.cfg -d
root 1892 50.9 1.0 19031092 1068868 pts/13 Sl 06:40 143:06 \_ /usr/local/jdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -server -Xms2048m -Xmx2048m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/jvm/tomcat.dump -Xloggc:/var/log/jvm/tomcat_real.log -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 1900 0.0 0.0 20508 2304 ? Ss 06:40 0:00 \_ crond
root 1903 0.0 0.0 11492 2688 pts/13 S+ 06:40 0:00 \_ /bin/bash
root 3512 13.5 3.9 3922968 3892780 ? Ssl 06:46 37:15 \_ /usr/local/bin/redis-server *:6380
root 4793 0.0 0.0 109424 1880 ? Ss 06:47 0:00 \_ nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
496 4794 0.0 0.0 109888 4516 ? S 06:47 0:00 \_ nginx: worker process
...
还有一种更有效的获取容器进程的方法,这种方法依赖docker程序,要是程序卡住就无法使用了。
docker top CONTAINERID
# docker top d14e1a6182eeed7c8f2a7c0a315a790a16bfbab1fdc7a73813cdeee494e8050a
UID PID PPID C STIME TTY TIME CMD
root 1761 4765 0 06:40 pts/13 00:00:00 /bin/bash /root/bin/autoboot.sh
root 1869 1761 0 06:40 ? 00:00:00 /usr/sbin/sshd
497 1877 1761 0 06:40 ? 00:00:00 /usr/sbin/nrpe -c /etc/nagios/nrpe.cfg -d
root 1892 1761 50 06:40 pts/13 02:33:35 /usr/local/jdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -server -Xms2048m -Xmx2048m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/jvm/tomcat.dump -Xloggc:/var/log/jvm/tomcat_real.log -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 1900 1761 0 06:40 ? 00:00:00 crond
root 1903 1761 0 06:40 pts/13 00:00:00 /bin/bash
root 3512 1761 13 06:46 ? 00:39:38 /usr/local/bin/redis-server *:6380
root 4793 1761 0 06:47 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
496 4794 4793 0 06:47 ? 00:00:00 nginx: worker process
还可以单独查看指定容器所属docker子进程的PID。
docker inspect -f '{{.State.Pid}}' CONTAINERID
# docker inspect -f '{{.State.Pid}}' d14e1a6182eeed7c8f2a7c0a315a790a16bfbab1fdc7a73813cdeee494e8050a
1761