# kubectl cp: Command Reference for CI/CD

> Reference for kubectl cp: copy files into or out of a container, the tar dependency that breaks distroless images, and a CI artifact-extraction example.

Source: https://latchkey.dev/learn/command-reference/kubectl-cp-command-cli-reference  
Updated: 2026-06-26

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
```

> Always set `--request-timeout` in CI. Without it an unreachable API server hangs until the job times out, which turns a thirty-second failure into a twenty-minute one.

## FAQ

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

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
