18

I have a list of PID's and I need to get their docker container name. Going the other direction is easy ... get PID of docker container by image name:

$ docker inspect --format '{{.State.Pid}}' {SOME DOCKER NAME}

Any idea how to get the name by PID?

slm
  • 15,396
  • 12
  • 109
  • 124
Nimrod007
  • 9,825
  • 8
  • 48
  • 71

4 Answers4

25

Something like this?

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

[EDIT]

Disclaimer This is for "normal" linux. I don't know anything useful about CoreOS, so this may or may not work there.

ivant
  • 3,909
  • 1
  • 25
  • 39
  • 1
    if only docker name needed can add | awk '{print $2}' in the end :) thanks – Nimrod007 Jun 25 '14 at 13:01
  • 2
    For my current docker version I had to change {{.ID}} to {{.Id}}, seems the docker inspect output has changed slightly! – David Jan 30 '15 at 08:55
  • 1
    What is the `${PID}` in the grep regex do? I just tried this and the grep is not working. – wheeler Dec 03 '17 at 18:43
  • @wheeler, it's the PID you're looking for. For example, if you're looking for PID 123, then you can `export PID=123` before running the above command. Or you can just replace `${PID}` with `123`. – ivant Feb 24 '21 at 12:03
  • For full list and name use `docker ps -q | xargs docker inspect --format '{{.State.Pid}}, {{.Id}} {{.Name}}'` – Mert Ülkgün Nov 17 '22 at 22:37
14

Because @Mitar's comment suggestion deserves to be a full answer:

To get container ID you can use:

cat /proc/<process-pid>/cgroup

Then to convert the container ID to docker container name:

docker inspect --format '{{.Name}}' "${containerId}" | sed 's/^\///'
Jay Taylor
  • 13,185
  • 11
  • 60
  • 85
5

... as a one-liner as well

PID=20168; sudo docker ps --no-trunc | grep $(cat /proc/$PID/cgroup | grep -oE '[0-9a-f]{64}' | head -1) | sed 's/^.* //'

Orsiris de Jong
  • 2,819
  • 1
  • 26
  • 48
Ritschie
  • 51
  • 1
  • 1
  • or even shorter PID=20168; sudo docker ps --no-trunc | grep $(grep -oE '[0-9a-f]{64}' /proc/$PID/cgroup | head -1) | sed 's/^.* //' – Ritschie Apr 13 '21 at 16:11
  • or even minimal faster :) sudo docker ps --no-trunc | grep $(head -1 /proc/$PID/cgroup | grep -oE '[0-9a-f]{64}') | sed 's/^.* //' a little explanation: --no-trunc shows the container-id in a 64Bit Hash containing only Numbers and Characters from "a" to "f" grep -oE '[0-9a-f]{64}' greps exactly for a 64 Bit hash and prints out the hash only – Ritschie Apr 13 '21 at 16:24
  • Please don't include additional information to your answer as a comment. Instead, please [edit] your answer to include this information in the answer itself, using [formatted-text](/editing-help#code). – Hoppeduppeanut Apr 14 '21 at 04:03
2

I use the following script to get the container name for any host PID of a process inside a container:

#!/bin/bash -e
# Prints the name of the container inside which the process with a PID on the host is.

function getName {
  local pid="$1"

  if [[ -z "$pid" ]]; then
    echo "Missing host PID argument."
    exit 1
  fi

  if [ "$pid" -eq "1" ]; then
    echo "Unable to resolve host PID to a container name."
    exit 2
  fi

  # ps returns values potentially padded with spaces, so we pass them as they are without quoting.
  local parentPid="$(ps -o ppid= -p $pid)"
  local containerId="$(ps -o args= -f -p $parentPid | grep docker-containerd-shim | cut -d ' ' -f 2)"

  if [[ -n "$containerId" ]]; then
    local containerName="$(docker inspect --format '{{.Name}}' "$containerId" | sed 's/^\///')"
    if [[ -n "$containerName" ]]; then
      echo "$containerName"
    else
      echo "$containerId"
    fi
  else
    getName "$parentPid"
  fi
}

getName "$1"
Mitar
  • 6,756
  • 5
  • 54
  • 86
  • Which version of Docker are you using? – Mitar Mar 12 '17 at 05:39
  • 4
    You can also use `cat /proc//cgroup` to get container ID, and then use `docker inspect --format '{{.Name}}' "$containerId" | sed 's/^\///'` to get name from ID. – Mitar Mar 12 '17 at 05:39
  • 1
    As a one-liner, using @Mitar 's idea: `docker inspect --format '{{.Name}}' "$(cat /proc/$PID/cgroup |head -n 1 |cut -d / -f 3)" | sed 's/^\///'` – Markus Kasten Jul 20 '17 at 12:00