5

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
kvaps
  • 223
  • 3
  • 9
  • 3
    `Docker` and `Cri-O` have similar syntax. Did you try to use `crictl inspect --output go-template --template '{{.info.pid}}' `? Output should be PID value of the container. – PjoterS Feb 26 '21 at 14:06
  • There is no key 'info' at all. – Lethargos May 22 '22 at 21:27

1 Answers1

1

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>
PjoterS
  • 615
  • 3
  • 11
  • This is probably correct answer. But I have no any info section in the output.I've tried latest version of crictl, so it is most probably docker runtime issue, could you provide your `crictl version` please? – kvaps Mar 01 '21 at 10:57
  • What output you have if you will use this command? I've tested this on my GKE 1.19 cluster: $ crictl version Version: 0.1.0 RuntimeName: containerd RuntimeVersion: 1.4.1 RuntimeApiVersion: v1alpha2 – PjoterS Mar 01 '21 at 11:02
  • 1
    Well, in my case I have an error ```FATA[0000] getting the status of the container "bac7563751e9a": failed to template data: template: tmplExecuteRawJSON:1:7: executing "tmplExecuteRawJSON" at <.info.pid>: map has no entry for key "info"``` Version: 0.1.0 RuntimeName: docker RuntimeVersion: 19.03.14 RuntimeApiVersion: 1.40.0 – kvaps Mar 02 '21 at 20:30
  • What output you have when you just use `crictl inspect ` ? – PjoterS Mar 03 '21 at 10:07