Docker find pid container internal process

I have docker containers. A process was launched inside them. On the main machine , the top command displays the pid of all processes running inside the containers.

How can I find the container in which the process works with this PID?

Thanks.

+5
source share
4 answers

Thanks to @Alex Past and @Stanislav for their help. But I did not receive full answers for me. I combined them. In conclusion, I received the following.

First

 pstree -sg <PID> 

where PID is the PID process from the top command

On output, I get the parent PID for the parent systemd process. This PID is the PID of the docker container.

After doing

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

where% PID% is the parent PID.

As a result, I have a container identifier CONKINER ID.

What i wanted

+8
source

I suppose you need something like this:

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

You can find all the parents for this process:

 pstree -sg <PID> 

This chain will contain a container

+1
source

You should be able to use exec for every running container that checks if pid exists. Of course, the same process identifier can exist in several containers. Here is a small bash script that looks for an executable process based on the supplied pid in each container:

 #!/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 instance:

 ./find-process.sh 1 
+1
source

Source: https://habr.com/ru/post/1260645/


All Articles