Question

How to copy a file from a running Docker container to host?

As a good practice, you should treat containers as immutable and not make any direct changes during runtime. However, there are some cases when you would need to copy a file from your container to your Docker host for a lot of possible reasons.

Here’s how to do that!


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
March 15, 2020
Accepted Answer

In order to copy a file from a running container to your host you first need to get your Docker container ID, you can do that with the following command:

  1. docker ps

Then once you have your container ID, you can run the following:

  1. docker cp container_id:/path/to/your_file.txt /path/on/your/host

Make sure to update the path to the file inside the container and the path on your host where you want to store the file.

You can also use the container name rather than the container ID.

Here is also a quick video on how to do that as well:

Hope that this helps! Regards, Bobby

hello, thank you, in my case I copied the logs to my host but I need they are permanent, in other words I want to the logs keep writing in my host, in that case what can I do? and the other solution is put a volume in the docker-compose and run again everything but that one is not that the case.

I hope you can help. greetings.

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.