How to Copy files from Docker Container to Host System


Copy Files from Docker Container to Host

If you have files in your docker container that you want to get into your host/local device, you can achieve it using the docker cp command.

Example:

Inside the docker container I have a file 20220901.log under the root directory, I want to get it to my host system,.

# pwd
/root

# ls
20220901.log

Copy file from Docker Container

% docker ps -a
CONTAINER ID   IMAGE     COMMAND     CREATED         STATUS                     PORTS     NAMES
1d2b384f31de   alpine    "/bin/sh"   6 minutes ago   Exited (0) 6 minutes ago             intelligent_noether

% docker cp 1d2b384f31de:/root/20220901.log

Note: You might get operation not permitted if you try to copy the file to the host directory where you do not have permission to write.

Also, if a path or file does not exist on the docker container you will get an error: No such container:path: or if the host path is invalid: invalid output path: directory does not exist

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap