Question

How to copy files and directories from and to a running Kubernetes Pod?

If you had to copy a file or a folder from one Linux server to another you would use either the scp command or the rsync command.

Here is how you could copy a file or a folder from and to a running Kubernetes Pod.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
October 11, 2020
Accepted Answer

In order to copy files and directories from a Pod, you could use the kubectl cp command.

The syntax is similar to the standard cp command, but you need:

kubectl cp source destination

Let’s go through a couple of scenarios!

Coping a file or a folder from your host to the Pod

In order to copy a file or a folder from your host to a running pod you can use the following command:

  • Syntax:
  1. kubectl cp /path/to/your_folder name-of-your-pod:/path/to/destination_folder
  • Example, here I will copy a file called index.html to the /var/www/html folder inside my my-lamp-server pod:
  1. kubectl cp /home/sammy/index.html my-lamp-server:/var/www/html/index.html

The above command will work with both a file and a folder. Unlike the cp command, you don’t have to specify the -r argument.

Coping a file or a folder from the Pod to your host

If there is a file inside your pod which you would like to copy to your local machine, you need to use the following command:

  • Syntax:
  1. kubectl cp name-of-your-pod:/path/to/your_folder /path/on_your_host/to/your_folder
  • Example, here I will copy my error log file from my-lamp-server to my local machine:
  1. kubectl cp my-lamp-server:/var/log/apache2/error.log /home/sammy/error.log

Copy from a specific container

In some cases you might have multiple containers running inside your Pod. In order to specify which one you want to copy from or to you just need to use the -c argument:

kubectl cp name-of-your-pod:/path/to/your_folder /path/on_your_host/to/your_folder -c some-container-name-here

Conclusion

For more information, make sure to check out the official documentation here:

https://kubectl.docs.kubernetes.io/pages/container_debugging/copying_container_files.html

For more information about Kubernetes, make sure to check out this introduction to Kubernetes tutorial:

https://www.digitalocean.com/community/tutorials/an-introduction-to-kubernetes

I hope that this helps! Regards, Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.