Pivert's Blog

Find the docker container running a particular process


Reading Time: < 1 minute

Sometimes you might want to find which container is running a particular process on your host, especially if you have many containers.

Identify the container ID of the running process

Use pstree and less to point to the process. In this example, I would like to know in which container runs the process containing the language keyword.

pstree -a -p | less +/language

Use pstree with:

  • -a to display the full path
  • -p is optional in this case. It shows the PID

And pipe it into less. Within less, you can search with / or you can add the +/{searchregex} command line argument to immediately search for the term.

Find the parent process and get the CT ID

If you have the process you’re looking for, press ‘u’ to go up and to display the ‘containerd-shim’.

  |   |-{containerd-shim},366349
  |   |-{containerd-shim},366532
  |   `-{containerd-shim},367402
  |-containerd-shim,464370 -namespace moby -id 401a4de534ade17e35857dae88261fd7acef85b2e9676965242c4aabddb183b3 -address/run/
  |   |-bash,464390 start.sh
  |   |   `-java,464469 -Xms512m -Xmx1g -cp languagetool-server.jar org.languagetool.server.HTTPServer --port 8010 --public ...
  |   |       |-{java},464470
  |   |       |-{java},464471
  |   |       |-{java},464472
  |   |       |-{java},464473

Copy the id of the container.

In some older linux distributions, the pstree -a option might not display the full line. In this case, just search with ps -ef and grep on the PID

In this example the CT ID is:

401a4de534ade17e35857dae88261fd7acef85b2e9676965242c4aabddb183b3

Inspect the container to get its name

Use docker inspect, and check for the name. Or use jq as in the example below

root:~#docker inspect 401a4de534ade17e35857dae88261fd7acef85b2e9676965242c4aabddb183b3 | jq '.[] | .["Name"]'
"/languagetool_languagetool.1.q7w4ez54k8ubxgstusdao550j"

We get it !

Like it ?

Get notified on new posts (max 1 / month)
Soyez informés lors des prochains articles