Copy Files from and to Kubernetes pods and Docker container

mfahlandt profile image Mario Updated on ・1 min read

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
kubectl cp mongo-0:/dump /local/dump

Copy Files to a pod

kubectl cp /local/path namespace/podname:path/to/directory 
kubectl cp /local/dump mongo-0:/dump

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
docker cp mongo-0:/dump /local/dump

Copy Files to a docker container

docker cp /local/path containerID:path/to/directory
docker cp /local/dump mongo-0:dump

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.

Posted on by:

mfahlandt profile



tech freak, organizer of GDG Munich Cloud, doing react, nodejs and a lot of cloud and kubernetes, docker stuff. Developer @ Königspunkt


markdown guide

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


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