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.xmlIn 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
kubectl Cheat Sheet: Commands for Everyday KubernetesA kubectl cheat sheet - get, describe, logs, apply, rollout, debug, and context commands for daily Kubernetes…
kubectl exec: Command Reference for CI/CDReference for kubectl exec: run a command inside a running container, the -- separator, container selection,…
kubectl logs: Command Reference for CI/CDReference for kubectl logs: stream container output, read previous-container logs for crash loops, select con…
kubectl get: Command Reference for CI/CDReference for kubectl get: list resources, output formats, label and field selectors, jsonpath extraction, an…