Skip to content

containerd and the ctr CLI — talking to the daemon directly

Use the ctr CLI to manage containerd from the host — pull images, create containers, attach to tasks, and debug what Docker or Kubernetes is doing underneath.

ctr is the low-level CLI shipped with containerd itself, exposing the daemon’s API directly without going through Docker or Kubernetes abstractions. When kubelet says “pull and run this image” and something fails in a way kubectl logs cannot explain, ctr is how you ask containerd what it actually thinks happened. It is not the daemon’s user-friendly face — that role belongs to nerdctl — but it is the operator’s diagnostic tool.

How to verify

A working ctr install responds to these probes:

ctr --version
ctr namespaces list
ctr -n k8s.io containers list | head -10
ctr -n k8s.io tasks list | head -10
ctr -n moby images list 2>/dev/null | head -10

The -n flag selects the namespace. Without it, ctr defaults to default. Kubernetes-created objects live in k8s.io; Docker-via-containerd objects live in moby. This namespace separation is why docker ps and ctr containers list can show entirely different things on the same host — they look in different namespaces by default.

What’s happening

containerd organises everything into namespaces (not Linux namespaces — gRPC API namespaces). Kubelet uses k8s.io, the Docker daemon uses moby, ctr and nerdctl default to default. Within a namespace there are images (immutable content-addressable layers), containers (the metadata that says “this image, this config, this rootfs”) and tasks (a running instance of a container, with a PID). A container without a task is stopped; a task without a container is an orphan.

ctr lets you create and inspect each of these directly. Where docker run is one user-facing verb, ctr decomposes it: ctr images pull → ctr containers create → ctr tasks start. This is closer to the OCI specification’s distinction between “container” (a directory bundle) and “task” (a running process).

The daemon’s socket is /run/containerd/containerd.sock. Both ctr and kubelet’s CRI plugin talk to it; they just use different namespaces.

How to use it

  1. List images across all namespaces — useful when “image not found” errors are confusing:
for ns in $(ctr namespaces list -q); do
  echo "=== namespace: $ns ==="
  ctr -n $ns images list -q
done
  1. Pull an image into the namespace kubelet uses. This is the right way to pre-warm a node’s image cache:
sudo ctr -n k8s.io images pull docker.io/library/nginx:alpine
sudo ctr -n k8s.io images list | grep nginx
  1. Run an image directly without going through Kubernetes — useful for verifying that a registry image actually works on this host:
sudo ctr -n test images pull docker.io/library/alpine:3.19
sudo ctr -n test run --rm -t docker.io/library/alpine:3.19 test-shell /bin/sh
# you are now in the container
  1. Inspect a running Kubernetes container at the containerd level. This shows the real OCI bundle, the actual runtime, and the actual cgroup paths:
sudo ctr -n k8s.io containers info <container-id>
sudo ctr -n k8s.io tasks ps <container-id>
sudo ctr -n k8s.io tasks metrics <container-id>
  1. Mount an image’s rootfs to the host without starting a container — invaluable for inspecting an image’s contents:
sudo ctr -n test images mount docker.io/library/nginx:alpine /mnt/nginx-rootfs
ls /mnt/nginx-rootfs/etc/nginx/
sudo ctr -n test images unmount /mnt/nginx-rootfs
  1. Stream task events in real time. This is what populates Kubernetes pod-state changes:
sudo ctr -n k8s.io events

Container creates, task starts, task exits with code, OOM kills — they all appear on this stream. Tail it during a flaky kubectl rollout to see what containerd is actually doing.

Common pitfalls

  • Running ctr containers list and seeing nothing while crictl ps shows pods. They are looking in different namespaces — always pass -n k8s.io when investigating Kubernetes state.
  • Mixing ctr and nerdctl against the same namespace. nerdctl adds its own metadata (labels, networks) that ctr does not honour; cross-tool operations can leave dangling state.
  • Using ctr run and expecting Docker-style ergonomics (port publishing, volume mounts, networking). ctr run is a thin OCI runner — it does not configure networking beyond host-net, and it does not publish ports.
  • Forgetting that ctr has no built-in CNI integration. Containers started with ctr run get the host network namespace by default — fine for testing, dangerous as a deployment pattern.
  • Pulling images with ctr images pull into the wrong namespace and being surprised Kubernetes does not see them. The namespace is the index.

Stack Harbor uses ctr as the diagnostic tool of choice on client Kubernetes nodes — see managed operations for the runbooks we keep for image-cache forensics, task-state debugging, and the cross-namespace audits we run when kubelet behaviour disagrees with the cluster state.