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



Have Questions? Post them here!
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.