简体   繁体   English

docker通过内部进程的pid查找容器

[英]docker find container by pid of inside process

I have docker containers.我有码头集装箱。 Inside them launched a process.在他们里面展开了一个过程。 From the host machine the command top outputs pid of all processes launched in within containers.从主机,命令top输出容器内启动的所有进程的 pid。

How can I find a container in which the process with this PID is running?如何找到一个容器,在该容器中运行具有此 PID 的进程?

Thank you.谢谢你。

Thank you @Alex Past and @Stanislav for the help.谢谢@Alex Past 和@Stanislav 的帮助。 But I did not get full answers for me.但我没有得到完整的答案。 I combined them.我把它们结合起来。
In summary I has got next.总之,我有下一个。

First首先

pstree -sg <PID>

where PID is the process's PID from the command top其中 PID 是来自命令top的进程的 PID

In output I am getting parent PID for the systemd parent process.在输出中,我获得了 systemd 父进程的父 PID。 This PID is docker container's PID.这个PID是docker容器的PID。

After I execute我执行后

docker ps -q | xargs docker inspect --format '{{.State.Pid}}, {{.Name}}' | grep "^%PID%"

where %PID% is this parent PID.其中%PID%是这个父 PID。

In result I have docker's CONTAINER ID .结果我有码头工人的CONTAINER ID

That's what I wanted这就是我想要的

我想你需要这样的东西:

 docker ps -q | xargs docker inspect --format '{{.State.Pid}}, {{.Name}}' | grep "%PID%"

You can find all parents for this process:您可以找到此过程的所有父项:

pstree -sg <PID>

This chain will be contains the container该链将包含容器

You should be able to use exec against each running container checking if the pid exists.您应该能够对每个正在运行的容器使用exec检查 pid 是否存在。 Of course the same process id could exists in more than one container.当然,同一个进程 id 可以存在于多个容器中。 Here is a small bash script that search for a running process based on the supplied pid in each container:这是一个小的 bash 脚本,它根据每个容器中提供的 pid 搜索正在运行的进程:

#!/bin/bash

for container in $(docker ps -q); do
  status=`docker exec $container ls /proc/$1 2>/dev/null`
  if [ ! -z "$status" ]; then
    name=`docker ps --filter ID=$container --format "{{.Names}}"`
    echo "PID: $1 found in $container ($name)"
    break;
  fi
done;

For example:例如:

./find-process.sh 1

You can cycle through the parent processes of the target process using ps -o ppid= and at each step check if the PID of the parent matches one of the containers.您可以使用ps -o ppid=循环浏览目标进程的父进程,并在每一步检查父进程的 PID 是否与容器之一匹配。

#!/bin/bash

targetpid=$1
parentpid=0

while [ $parentpid != 1 ]; do
    parentpid=$(ps -o ppid= $targetpid)
    docker ps -q | xargs docker inspect --format '{{.State.Pid}}, {{.Name}}' | grep "^$parentpid"
    targetpid="$parentpid"
done

I kinda combined all of these and wrote this two liner.我有点结合所有这些并写了这两个班轮。 Hopefully useful to someone.希望对某人有用。

#!/bin/bash
SCAN_PID=`pstree -sg $1 |  head -n 1 | grep -Po 'shim\([0-9]+\)---[a-z]+\(\K[^)]*'`
docker ps -q | xargs docker inspect --format '{{.State.Pid}}, {{.Name}}' | grep "${SCAN_PID}"

First line finds the container entry script and feeds it to the docker inspect.第一行找到容器入口脚本并将其提供给 docker inspect。

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

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