Skip to content
Latchkey

kubectl exec: Usage, Options & Common CI Errors

Run a command - or open a shell - inside a live container.

kubectl exec executes a process in an already-running container. It is how you poke at a live pod from CI: check a config file, hit an internal endpoint, or run a one-off migration.

What it does

kubectl exec POD -- CMD runs CMD inside the pod's first container; -c selects another. -i keeps stdin open and -t allocates a TTY, so -it CMD gives an interactive shell. Everything after -- is passed verbatim to the container, not parsed by kubectl.

Common usage

Terminal
kubectl exec my-pod -- cat /etc/config.yaml
kubectl exec -it my-pod -- sh
kubectl exec -it my-pod -c app -- bash
kubectl exec my-pod -- env | grep DATABASE

Common errors in CI

"OCI runtime exec failed: ... \"sh\": executable file not found" means the image is distroless/scratch and has no shell - exec a static binary the image does ship, or use kubectl debug to attach an ephemeral container with tools. "error: unable to upgrade connection: pod does not exist" usually means the pod already terminated (common when exec-ing into a Job pod that exited). In non-interactive CI, never pass -t: allocating a TTY without a real terminal mangles output and can hang the step.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →