Skip to content
LatchkeyLatchkey home

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.

Using this in CI

A runner has no kubeconfig, no cached context, and no interactive auth. Every kubectl invocation in CI needs the context supplied explicitly, and most confusing CI failures here are the command running against the wrong cluster or no cluster at all.

Terminal
# never rely on the ambient context on a runner
kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get pods

# confirm what you are actually connected to before mutating anything
kubectl config current-context
kubectl cluster-info

# fail fast instead of hanging on an unreachable API server
kubectl --request-timeout=30s get nodes

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.

Frequently asked questions

kubectl cp: Command Reference for CI/CD?
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.
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.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card