23

I am trying to copy files from the pod to local using following command:

kubectl cp /namespace/pod_name:/path/in/pod /path/in/local

But the command terminates with exit code 126 and copy doesn't take place.

Similarly while trying from local to pod using following command:

kubectl cp /path/in/local /namespace/pod_name:/path/in/pod

It throws the following error:

OCI runtime exec failed: exec failed: container_linux.go:367: starting container process caused: exec: "tar": executable file not found in $PATH: unknown

Please help through this.

kkpareek
  • 450
  • 1
  • 5
  • 15

5 Answers5

26

kubectl cp is actually a very small wrapper around kubectl exec whatever tar c | tar x. A side effect of this is that you need a working tar executable in the target container, which you do not appear to have.

In general kubectl cp is best avoided, it's usually only good for weird debugging stuff.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • 2
    Nice! So at least we can `kubectl exec whatever cat c > x` or `kubectl exec whatever base64 c | base64 --decode > x` – Sassa NF May 14 '21 at 10:04
14

kubectl cp requires the tar to be present in your container, as the help says:

!!!Important Note!!! Requires that the 'tar' binary is present in your container image. If 'tar' is not present, 'kubectl cp' will fail.

Make sure your container contains the tar binary in its $PATH

chresse
  • 5,486
  • 3
  • 30
  • 47
14

An alternative way to copy a file from local filesystem into a container:

cat [local file path] | kubectl exec -i -n [namespace] [pod] -c [container] "--" sh -c "cat > [remote file path]"
RiveN
  • 2,595
  • 11
  • 13
  • 26
Yoni Sade
  • 150
  • 1
  • 5
11

Useful command to copy the file from pod to local

kubectl exec -n <namespace> <pod> -- cat <filename with path>  > <filename>
Dinesh Verma
  • 115
  • 1
  • 3
1

For me the cat worked like this:

cat <file name> | kubectl exec -i <pod-id> -- sh -c "cat > <filename>"

Example:

cat file.json | kubectl exec -i server-77b7976cc7-x25s8 -- sh -c "cat > /tmp/file.json"

Didn't need to specify namespace since I run the command from a specific project, and since we have one container, didn't need to specify it

Tal Haham
  • 1,500
  • 12
  • 16