kubectl exec: Command Reference for CI/CD
Run a command inside a live container from your pipeline.
kubectl exec runs a process in an already-running container: check a config file, hit an internal endpoint, or run a one-off migration. This reference covers the flags and the TTY rule that trips up non-interactive CI.
Common flags and usage
- -- <cmd>: everything after -- runs verbatim in the container
- -i, --stdin: keep stdin open (use when piping input)
- -t, --tty: allocate a TTY (interactive shells only)
- -c <container>: select a container in a multi-container pod
- kubectl exec POD -- CMD: non-interactive, the CI-safe form
Example
shell
# Run a DB migration inside the app container, no TTY
kubectl exec deploy/web -c app -- \
/app/bin/migrate --to=${SCHEMA_VERSION}
kubectl exec my-pod -- cat /etc/config.yamlIn CI
Never pass -t in a pipeline: allocating a TTY without a real terminal mangles output and can hang the step. Keep -i only if you actually pipe input. A distroless image with no shell needs you to exec a static binary it ships, not sh.
Key takeaways
- Drop -t in CI; a TTY without a terminal hangs or corrupts output.
- Everything after -- is passed verbatim to the container.
- Distroless images have no shell; exec a binary the image ships.
Related guides
kubectl Cheat Sheet: Commands for Everyday KubernetesA kubectl cheat sheet - get, describe, logs, apply, rollout, debug, and context commands for daily Kubernetes…
kubectl logs: Command Reference for CI/CDReference for kubectl logs: stream container output, read previous-container logs for crash loops, select con…
kubectl cp: Command Reference for CI/CDReference for kubectl cp: copy files into or out of a container, the tar dependency that breaks distroless im…
kubectl port-forward: Command Reference for CI/CDReference for kubectl port-forward: tunnel a local port to a pod or service for CI smoke tests, the readiness…