Skip to content
Latchkey

kubectl cp: Command Reference for CI/CD

Copy files into or out of a running container.

kubectl cp moves files between your machine and a container, commonly to pull a generated artifact (a coverage or test report) out of a CI pod. This reference covers the path syntax and the tar requirement that trips up minimal images.

Common flags and usage

  • cp <pod>:/path ./local: copy out of a pod
  • cp ./local <pod>:/path: copy into a pod
  • cp <ns>/<pod>:/path ./local: explicit namespace form
  • -c <container>: select a container in a multi-container pod
  • Requires a tar binary inside the target container

Example

shell
# Pull a JUnit report out of the test pod
kubectl cp ci/test-runner:/app/report.xml ./report.xml -c app

# Stream a single file when the image has no tar
kubectl exec test-runner -- cat /app/report.xml > report.xml

In CI

cp streams a tar archive through the container, so a distroless or scratch image without tar fails with "tar: not found". For a single file, stream it with kubectl exec ... -- cat > file instead. Create the destination directory first; cp does not make missing parent paths.

Key takeaways

  • cp needs tar inside the container; distroless images break it.
  • For one file, kubectl exec ... -- cat is a tar-free alternative.
  • The destination parent directory must already exist.

Related guides

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