DEV Community

loading...

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

Discussion (3)

pic
Editor guide
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...