Skip to content
LF Cert Trainer

Lesson 5 · Section 2: Core Concepts

Lesson 5: Docker vs containerd: what really runs your containers

Why Kubernetes talks to containerd instead of Docker, what CRI and OCI are, how a Pod becomes processes on a node, and how to debug a node with crictl when kubectl cannot help. · 20 min read · 5 min video

1/12Docker vs containerd
0:00 / 5:21

Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.

Why this matters

Most people meet containers through Docker, so it is natural to assume that Kubernetes "runs Docker containers". It used to. Today a typical node has no Docker on it at all, and the docker command you may reach for when something breaks is simply not there.

That raises three questions, and this lesson answers each of them:

  1. If Docker is gone, what runs the containers?
  2. Why did the images you built with Docker keep working?
  3. What do you type on a node instead of docker ps?

What Docker really is

What is inside DockerDocker EngineREST APIgRPCvia a shimcreatesdocker CLIbuild, run, pushdockerdAPI, build, volumescontainerdimages, lifecycleruncOCI runtimeContainernamespaces, cgroups
What is inside Docker. Docker is a stack, not one program. The part that actually runs containers is containerd, which calls runc. Everything to the left of containerd is tooling for people.

Parts:

  • docker CLI (build, run, push)
  • dockerd (API, build, volumes), in Docker Engine
  • containerd (images, lifecycle), in Docker Engine
  • runc (OCI runtime), in Docker Engine
  • Container (namespaces, cgroups)

Connections:

  • docker CLI to dockerd: REST API
  • dockerd to containerd: gRPC
  • containerd to runc: via a shim
  • runc to Container: creates

"Docker" is not one program. It is a stack, and each layer has a different job:

LayerWhat it doesDoes Kubernetes need it?
docker CLICommands for people: build, run, push, composeNo. Kubernetes has its own API and kubectl
dockerd (the Docker daemon)REST API, image builds, volumes, networks, registry loginsNo. Kubernetes has volumes, CNI and image pull secrets of its own
containerdPulls and stores images, creates and supervises containersYes
runcStarts one container: sets up namespaces and cgroups, then runs the processYes

containerd was written by Docker as the core of Docker Engine, then split out and donated to the CNCF in 2017, where it is now a graduated project. runc was donated even earlier, as the reference implementation of the Open Container Initiative (OCI) runtime specification.

So when Kubernetes "dropped Docker", it did not swap one runtime for a rival. It stopped going through the top two layers and kept talking to the bottom two, which had been doing the real work all along.

How Kubernetes talks to a runtime

The kubelet's path to a container, then and nowCRIDocker APICRI (gRPC on a Unix socket)kubeletup to v1.23dockershiminside the kubeletdockerdDocker Enginecontainerdrunckubeletv1.24 and latercontainerdCRI plugin built inruncOCI runtime
The kubelet's path to a container, then and now. Docker never spoke CRI, so the kubelet carried a translator called dockershim. Removing it in v1.24 cut two hops: the kubelet now talks straight to the containerd that Docker was using all along.

Parts:

  • kubelet (up to v1.23)
  • dockershim (inside the kubelet)
  • dockerd (Docker Engine)
  • containerd
  • runc
  • kubelet (v1.24 and later)
  • containerd (CRI plugin built in)
  • runc (OCI runtime)

Connections:

  • kubelet to dockershim: CRI
  • dockershim to dockerd: Docker API
  • dockerd to containerd
  • containerd to runc
  • kubelet to containerd: CRI (gRPC on a Unix socket)
  • containerd to runc

CRI, the plug

In the first Kubernetes releases, the kubelet contained code that called Docker directly. When other runtimes appeared (rkt was the first), each one needed more special code inside the kubelet. That does not scale, so in Kubernetes v1.5 (December 2016) the project defined the Container Runtime Interface (CRI): a gRPC API, served by the runtime on a Unix socket, with two services:

  • RuntimeService: Pod sandboxes and containers (RunPodSandbox, CreateContainer, StartContainer, StopContainer, ExecSync, and so on).
  • ImageService: images (PullImage, ListImages, RemoveImage).

Any runtime that serves those two services can run under the kubelet. The kubelet does not know or care what is behind the socket.

dockershim, the adapter that had to go

Docker predates CRI, and it never implemented it, because Docker is built for people and not for orchestrators. To keep Docker working, the Kubernetes maintainers wrote an adapter, dockershim, and carried it inside the kubelet. Every call went kubelet → dockershim → dockerd → containerd → runc.

That was a burden for two reasons: the Kubernetes project had to maintain code for one vendor's product, and new runtime features (cgroups v2, user namespaces) had to be wired through Docker before Kubernetes could use them. Dockershim was deprecated in v1.20 and removed in v1.24 (May 2022). The next lesson, on the Docker deprecation, covers what that meant for running clusters.

OCI, the reason nothing broke

Two specifications from the Open Container Initiative make runtimes interchangeable:

  • The image specification defines what an image is: a manifest, a config and a set of layer tarballs.
  • The runtime specification defines how to start a container from an unpacked image.

docker build produces OCI images. containerd pulls and runs OCI images. That is the whole reason an image built with Docker on a laptop runs on a containerd node without any change: there is no such thing as a "Docker image" that only Docker can run.

The runtimes you can choose

One interface, several runtimesCRICRICRIkubeletCRI clientcontainerdcontainerd.sockCRI-Ocrio.sockcri-dockerdcri-dockerd.sockDocker EnginedockerdruncOCI runtimeContainersLinux processes
One interface, several runtimes. The kubelet is a CRI client. Any runtime that serves CRI on a socket will do, and they all end at an OCI runtime such as runc.

Parts:

  • kubelet (CRI client)
  • containerd (containerd.sock)
  • CRI-O (crio.sock)
  • cri-dockerd (cri-dockerd.sock)
  • Docker Engine (dockerd)
  • runc (OCI runtime)
  • Containers (Linux processes)

Connections:

  • kubelet to containerd: CRI
  • kubelet to CRI-O: CRI
  • kubelet to cri-dockerd: CRI
  • cri-dockerd to Docker Engine
  • containerd to runc
  • CRI-O to runc
  • Docker Engine to runc
  • runc to Containers
RuntimeWhat it isDefault CRI socket
containerdGeneral-purpose runtime with a built-in CRI plugin. The usual choice for kubeadm, and what most managed clusters and k3s useunix:///run/containerd/containerd.sock
CRI-OA runtime built only for Kubernetes, released in step with it. Default in OpenShiftunix:///var/run/crio/crio.sock
Docker Engine + cri-dockerdThe dockershim code moved out of the kubelet into a separate adapter, for people who must keep Docker on their nodesunix:///run/cri-dockerd.sock

The kubelet finds its runtime through one setting, the container runtime endpoint. kubeadm detects the socket when you run kubeadm init or kubeadm join; if a node has more than one runtime installed you must choose with --cri-socket.

What happens on the node when a Pod starts

What containerd builds for one PodPod sandboxRunPodSandboxstartsruncruncPullImagekubeletsees a new Podcontainerdnamespace k8s.ioshimone per Podpauseholds the Pod IPnginxapp containerImage storepulled layers
What containerd builds for one Pod. A Pod becomes a sandbox (the pause container, which holds the network namespace) plus one container per entry in the Pod spec, all watched by one shim process.

Parts:

  • kubelet (sees a new Pod)
  • containerd (namespace k8s.io)
  • shim (one per Pod)
  • pause (holds the Pod IP), in Pod sandbox
  • nginx (app container), in Pod sandbox
  • Image store (pulled layers)

Connections:

  • kubelet to containerd: RunPodSandbox
  • containerd to shim: starts
  • shim to pause: runc
  • shim to nginx: runc
  • Image store to containerd: PullImage

Walk through it once and crictl output will make sense forever:

  1. The kubelet learns that a Pod was scheduled to its node and calls RunPodSandbox.
  2. containerd starts a shim process for the Pod (containerd-shim-runc-v2). The shim is the parent of the Pod's containers and keeps their stdio and exit codes.
  3. The shim uses runc to start the pause container. It does nothing; it exists to hold the Pod's network namespace, so the Pod keeps its IP while app containers restart. The CNI plugin then wires that namespace up.
  4. The kubelet calls PullImage for each image the node does not have yet.
  5. For each container in the Pod spec, the kubelet calls CreateContainer and StartContainer. runc starts the process inside the sandbox's namespaces, with the cgroup limits from the Pod spec.

This is why a Pod with one container shows up on the node as one sandbox and one container, and why crictl pods and crictl ps are separate commands.

Under the hood · Details that explain odd behaviour
  • Restarting containerd does not restart your Pods. Containers are children of their shim, not of containerd. You can systemctl restart containerd after changing its config and running containers keep running.
  • containerd has its own namespaces, which have nothing to do with Kubernetes namespaces. Everything the kubelet creates lives in the containerd namespace k8s.io; Docker Engine uses moby. That is why a bare ctr containers ls on a busy node prints nothing: it looks in the namespace default.
  • docker build on a node does not feed Kubernetes. Even if Docker is installed next to containerd, an image built or pulled with Docker is stored for Docker, not in the k8s.io namespace the kubelet looks in. The Pod reports ErrImagePull unless you push the image to a registry or import it with ctr -n k8s.io images import app.tar.
  • Container logs are files. The runtime writes each container's output to /var/log/pods/<namespace>_<pod>_<uid>/<container>/<restart>.log, and kubectl logs asks the kubelet to read that file. When the API server is down you can read the file directly.
  • The Docker socket is gone. Anything that mounted /var/run/docker.sock from the host (old CI agents that ran docker build in a Pod, some monitoring agents) breaks on a containerd node. Build with a tool that needs no daemon, such as BuildKit or Kaniko.

The command line tools

Three command line tools, two doorsctrships with containerdnerdctlDocker-like CLIcrictldebug CRI runtimescontainerd APIall namespacesCRI socketcontainerd or CRI-Okubeletuses the same socket
Three command line tools, two doors. ctr and nerdctl use containerd's own API. crictl uses the CRI socket, the same door the kubelet uses, so it sees exactly what Kubernetes sees.

Parts:

  • ctr (ships with containerd)
  • nerdctl (Docker-like CLI)
  • crictl (debug CRI runtimes)
  • containerd API (all namespaces)
  • CRI socket (containerd or CRI-O)
  • kubelet (uses the same socket)

Connections:

  • ctr to containerd API
  • nerdctl to containerd API
  • crictl to CRI socket
  • kubelet to CRI socket
ToolComes fromMeant forTalks to
ctrcontainerdDebugging containerd itself. Few features, unfriendly syntaxcontainerd's own API
nerdctlcontainerd (separate install)A general Docker replacement: same commands, plus build and composecontainerd's own API
crictlKubernetes (cri-tools)Debugging a Kubernetes node. Works with every CRI runtimeThe CRI socket

crictl is the one to learn. It uses the same socket and the same API as the kubelet, so it shows Pods and containers exactly as Kubernetes sees them, and it works the same on containerd and CRI-O.

Most of what you know from Docker carries over:

DockercrictlNotes
docker ps -acrictl ps -aAdd --name, --state or --pod to filter
docker imagescrictl images
docker pullcrictl pull
docker rmicrictl rmicrictl rmi --prune removes unused images
docker logscrictl logsTakes a container ID; -f and --tail work
docker exec -itcrictl exec -it
docker inspectcrictl inspectcrictl inspecti for images, crictl inspectp for Pods
docker statscrictl stats
docker infocrictl infoShows runtime status and config, including the cgroup driver
(no equivalent)crictl podsLists Pod sandboxes; filter with --name and --namespace
docker build, push, tag(no equivalent)crictl is a debugging tool, not a build tool

Using it

Example · Find out which runtime a cluster uses

From anywhere with kubectl, the last column of the wide node list names the runtime and its version:

kubectl get nodes -o wide
NAME   STATUS   ROLES           VERSION   ...   CONTAINER-RUNTIME
cp1    Ready    control-plane   v1.34.2   ...   containerd://2.1.4
w1     Ready    <none>          v1.34.2   ...   containerd://2.1.4

On the node itself, check the service and the socket the kubelet was told to use:

systemctl status containerd --no-pager
grep -r containerRuntimeEndpoint /var/lib/kubelet/config.yaml
cat /var/lib/kubelet/kubeadm-flags.env      # older kubeadm versions keep the flag here

Example · Point crictl at the runtime

Without configuration, crictl tries a list of well-known sockets and prints a warning each time, or fails with a permission error because the socket belongs to root. Tell it once where to connect, then run it with sudo:

# /etc/crictl.yaml
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
sudo crictl version          # proves the connection: prints the runtime name and version
sudo crictl info | grep -i -A3 cgroup

For one command only, the flag does the same: crictl --runtime-endpoint unix:///run/containerd/containerd.sock ps.

Example · The API server is down: debug without kubectl

kubectl answers The connection to the server ... was refused. The control plane runs as static Pods, so the containers are on the control plane node, and the runtime can show them even though the API cannot.

sudo crictl ps -a --name kube-apiserver     # -a includes exited containers
sudo crictl logs <container-id>              # the reason it exited is usually the last line
sudo crictl pods --name kube-apiserver       # is the kubelet still creating the sandbox?

A high number in the ATTEMPT column with the state Exited means the container starts and crashes in a loop: read its logs. No container at all means the kubelet could not even create it: the manifest in /etc/kubernetes/manifests/ is probably invalid YAML, so read the kubelet's log with journalctl -u kubelet --since "5 min ago" instead.

Example · Follow one Pod from the API down to the process

The Pod web in the namespace shop runs on node w1. Find its sandbox, its container and its process there:

sudo crictl pods --namespace shop --name web          # the sandbox: note the POD ID
sudo crictl ps --pod <pod-id>                          # the containers inside it
sudo crictl inspect <container-id> | grep -m1 '"pid"'  # the process ID on the host
ps -fp <pid>                                           # an ordinary Linux process

The last command is the point of the whole lesson: a container is a normal process on the node, started by runc with its own namespaces and cgroup.

Tricks

Common mistakes

  • Reaching for docker ps on a node. On a containerd node the command is missing, and where Docker is installed beside containerd it lists Docker's own containers, not the Pods. Use crictl.
  • Thinking images must be rebuilt. Images built with Docker are OCI images. Nothing about them changes when the node runs containerd.
  • Fixing a Pod with crictl. Stopping or removing a container by hand only makes the kubelet recreate it. Fix the cause (the manifest, the image, the config) and let the kubelet do the rest.
  • Forgetting -a. crictl ps lists running containers only. A crashed control plane component is not running, so without -a it looks as if it never existed.
  • Confusing the two kinds of namespace. crictl pods --namespace filters by Kubernetes namespace. ctr -n and nerdctl -n choose a containerd namespace, which for Kubernetes is always k8s.io.
  • Editing containerd's config and not restarting it. Changes to /etc/containerd/config.toml apply after sudo systemctl restart containerd. Running Pods survive the restart.

Recap

  • Docker is a stack: CLI → dockerd → containerd → runc. Kubernetes only needs the last two.
  • The kubelet talks to runtimes through CRI; Docker never implemented it, so dockershim translated until it was removed in v1.24.
  • Images built with Docker still run, because images and runtimes follow the OCI specifications.
  • On the node a Pod is a sandbox (the pause container) plus its containers, supervised by a shim.
  • crictl sees what the kubelet sees and works when the API server is down: ps -a, logs, pods, inspect.

Official docs

Quiz: check what you learned

5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.

Sign in to take the quiz and keep your score.

Practise: try it on a real cluster

A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.

  • Ask the container runtime on a node directly with crictl: find the process behind a container, read the logs of a container that already exited, and remove an image from one node only.

    Investigate · Level 2 · standard · target 20 min

Discussion

Learners ask and answer questions here. Sign in to read and join the discussion.