Skip to content

kubectl exec: cannot exec in a stopped container – now what?

Kubernetestested on RKE2 v1.37.2

kubectl exec needs a running container – and a program in the image it can start. If a distroless container crashes right away, exec fails twice over. The way out is a copy of the Pod with a different image that sees the same volumes.

Symptom

kubectl -n tag06 exec deploy/dns-test -- ls /etc/coredns
error: Internal error occurred: ... OCI runtime exec failed: exec failed: cannot exec
  in a stopped container

The container is not running, and even if it were: the image has neither sh nor ls. An exec with a different shell doesn't help either.

Diagnosis

The status and the log of the crashed run show why the container is down.

kubectl -n tag06 get pods
kubectl -n tag06 logs deploy/dns-test
NAME                        READY   STATUS             RESTARTS      AGE
dns-test-6f6cb5574b-v5blz   0/1     CrashLoopBackOff   1 (12s ago)   21s
loading Caddyfile via flag: open /etc/coredns/Corefile: no such file or directory
maxprocs: Leaving GOMAXPROCS=2: CPU quota undefined

Causes

CrashLoopBackOffThe container is stopped

exec starts a process in a running container. Between two crashes there is none – the kubelet is waiting for the next restart.

DistrolessThere is no shell in the image

Distroless images contain only the application. kubectl debug brings the tooling along as a separate image. For a crashing Pod that is a copy in which busybox replaces the container and just sleeps – volumes and env stay the same.

P=$(kubectl -n tag06 get pod -l app=dns-test -o jsonpath='{.items[0].metadata.name}')
kubectl -n tag06 debug $P --copy-to=dns-kopie --set-image=coredns=busybox:1.37 \
  --container=coredns -- sleep 3600
kubectl -n tag06 wait pod/dns-kopie --for=condition=Ready
kubectl -n tag06 exec dns-kopie -- ls -l /etc/coredns/
pod/dns-kopie condition met
total 0
lrwxrwxrwx    1 root     root            15 Nov 20 07:37 corefile -> ..data/corefile

Cause in the courseWrong file name from the ConfigMap

The ConfigMap key is called corefile, but CoreDNS expects the file Corefile. Linux is case-sensitive. Only the copy shows it.

Fix

Delete the copy, correct the key in the ConfigMap and roll out again. CoreDNS then starts.
kubectl -n tag06 delete pod dns-kopie
sed -i 's/^  corefile: |/  Corefile: |/' dns-test.yaml
kubectl apply -f dns-test.yaml
kubectl -n tag06 rollout restart deploy/dns-test
kubectl -n tag06 rollout status deploy/dns-test
kubectl -n tag06 logs deploy/dns-test | head -3
maxprocs: Leaving GOMAXPROCS=2: CPU quota undefined
.:1053
CoreDNS-1.14.7
▶
Break it yourself and find it – free
Debugging Kubernetes, day 6: inspecting containers without a shell, copying Pods with a different image and reaching the node without SSH – plus the limits of the profiles.
Go to day 6 →

FAQ

Ephemeral container or copy?

If the Pod runs and only lacks a shell, an ephemeral container with --target is enough. If it crashes right away, use the copy with --copy-to, --set-image and --container.

Why does the copy get no Service traffic?

The copy has no labels. It belongs to no ReplicaSet and gets no Service traffic, unless you use --keep-labels.

Related error patterns

More