Skip to content
Latchkey

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.yaml

In 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →