简体   繁体   English

通过系统PID获取内部docker PID

[英]Get internal docker PID by system PID

After I use exec command inside docker container I can get the PID with exec inspect . 在docker容器内使用exec命令后,可以通过exec inspect获得PID。 The problem is that this ID is not local to the container but a system PID. 问题在于此ID并不是容器本地的,而是系统PID。 So I would get something like 22620 while the PID inside docker container is 695 . 因此,当docker容器中的PID为695时,我会得到类似22620的信息。

I know that docker uses process namespaces to isolate its processes. 我知道docker使用进程名称空间来隔离其进程。 So I have tried to get the internal PID with cat /proc/22620/status | grep NSpid 所以我试图用cat /proc/22620/status | grep NSpid获取内部PID cat /proc/22620/status | grep NSpid cat /proc/22620/status | grep NSpid . cat /proc/22620/status | grep NSpid This indeed gave me the PID I was looking for: 这确实给了我要寻找的PID:

NSpid: 22620 695

But I need to parse this string to get the PID. 但是我需要解析此字符串以获取PID。 Is there a proper way to get the PID used by the docker container? 有没有适当的方法来获取docker容器使用的PID?

PS. PS。

I need this process ID to kill the process started by other exec call. 我需要此进程ID来杀死由其他exec调用启动的进程。 I cannot kill the system process as it is owned by root and I cannot use sudo. 我无法杀死系统进程,因为它是root拥有的,并且不能使用sudo。

Och. OCH。 Use cut to filter a section from a line. 使用“ cut从线中过滤出一部分。

grep NSpid /proc/24918/status | cut -f3

Will give 695 from the example. 从示例中将得出695

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM