DEV Community

Mario
Mario

Posted on • Updated on

Copy Files from and to Kubernetes pods and Docker container

I often want to have some database dumps from my mongo also in my local Setup. To achieve this is pretty easy.
You can either copy files or whole folders.
What we need is the kubectl command line tool.

Copy Files from a pod to your machine

kubectl cp {{namespace}}/{{podname}}:path/to/directory /local/path
eg:
kubectl cp mongo-0:/dump /local/dump
Enter fullscreen mode Exit fullscreen mode

Copy Files to a pod

kubectl cp /local/path namespace/podname:path/to/directory 
eg:
kubectl cp /local/dump mongo-0:/dump
Enter fullscreen mode Exit fullscreen mode

The amazing hint on this is, this works as well with docker.
Just change kubectl to docker and it will work as well.

Copy Files from a docker container to your machine

docker cp containerID:/path/to/directory /local/path
eg:
docker cp mongo-0:/dump /local/dump
Enter fullscreen mode Exit fullscreen mode

Copy Files to a docker container

docker cp /local/path containerID:path/to/directory
eg:
docker cp /local/dump mongo-0:dump
Enter fullscreen mode Exit fullscreen mode

Why i write this obvious post? I'm tiered looking up the correct syntax now and then and just looking at my blog post speed things up.

Top comments (4)

Collapse
 
world2mark profile image
Mark Zlamal

Awesome and simple! Works perfectly in OpenShift as well ("oc cp ....").

Collapse
 
p4prawin profile image
Pravin Mane

when files is copied to pod, pod gets restarted.
could not understand the problem
any idea?

Collapse
 
bobbyiliev profile image
Bobby Iliev

It is possible that the pod is running out of disk space. What do the logs say? You can check the logs with this command.

Collapse
 
shreygarglti profile image
ShreyGargLTI

How to do the same via code with "@kubernetes/client-node" i.e., the official Kubernetes client library for javascript.
github.com/kubernetes-client/javas...