Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker find container by pid of inside process

Tags:

docker

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.

How can I find a container in which the process with this PID is running?

Thank you.

like image 575
Alexcei Shmakov Avatar asked Dec 01 '16 12:12

Alexcei Shmakov


People also ask

How do you find the PID of a container?

Find the running container's ID by using the docker ps command. Find the PID number of the first process in the running container by running the docker inspect command. Enter the running container by using the nsenter command.

How can I see what process is running inside a docker container?

Like it was mentioned, if you are already inside of a container, then just use ps -eaf command to see the running processes.

How do I find the internal IP address of a docker container?

You can easily get the IP address of any container if you have the name or ID of the container. You can get the container names using the "Docker ps -a" command. This will list all the existing containers.


1 Answers

Thank you @Alex Past and @Stanislav for the help. 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

In output I am getting parent PID for the systemd parent process. This PID is docker container's PID.

After I execute

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

where %PID% is this parent PID.

In result I have docker's CONTAINER ID.

That's what I wanted

like image 144
Alexcei Shmakov Avatar answered Sep 19 '22 15:09

Alexcei Shmakov