DEV Community

Mario
Mario

Posted on • Edited on

15 2

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.

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

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

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay