How to find out PID of the container using crictl

Kubernetes is deprecating Docker as CRI engine. Now there is containerd and CRI-O, which can be used as an alternative to it. Both can be managed via crictl from cri-tools.

I need some common way to find the PID of running container.

Actually I need a crictl equivalent to the docker command:

# docker inspect 86cd8d605c2da -f '{{ .State.Pid }}'
9625

As I mention in comment section, to achieve what you need using CRI you have also use inspect command.

Steps to Achieve container PID

  1. List containers to get Container ID.
$ crictl ps
CONTAINER           IMAGE               CREATED             STATE               NAME                   ATTEMPT             POD ID
cdb3feac5bdd3       35c43ace92162       3 minutes ago       Running             nginx                  0                   940b5d97fb46a
0d84f965a1c8d       6266988902813       29 minutes ago      Running             prometheus-to-sd       0                   11dad991a79e4
437c1e31d5ff1       ccbe64d6d9477       29 minutes ago      Running             metadata-agent-nanny   0                   74cbad8857254
  1. Inspect specific container
$ crictl inspect cdb3feac5bdd3
{
  "status": {
    "id": "cdb3feac5bdd3981d58fb3cd2fc08cf53bbff326a38c95a77e7ec43a80fa9713",
    "metadata": {
      "attempt": 0,
      "name": "nginx"

In below part, you will be able to find this container PID.

"info": {
    "sandboxID": "940b5d97fb46a3c6efdf256f751cab3f3a282150efd4cdef969692d03deb4829",
    "pid": 15701,

Above (default) output is in Go-Template format (you can also chose YAML or JSON format).

$ crictl inspect
NAME:
   crictl inspect - Display the status of one or more containers

OPTIONS:
   --output value, -o value  Output format, One of: json|yaml|go-template|table

To get what you need you just need specify path to this information.

$ crictl inspect --output go-template --template '{{.info.pid}}' cdb3feac5bdd3
15701

Solution

Use below command with containerID to get container PID.

$ crictl inspect --output go-template --template '{{.info.pid}}' <yourContainerID>