How to copy files from host to Docker container

Views: 7
0 0
Read Time:1 Minute, 25 Second

This is a small how to for helping you copying files from your host filesystem to your container. Again this is not a normal practice but if you are debugging some stuff and want to copy a tool or a library or a file it can help.

Let’s assume we have this situation

$> docker ps
CONTAINER ID   IMAGE            COMMAND     STATUS         NAMES
5bdfd620b191   p2.7.18:latest   "/bin/sh"   Up 3 minutes   test-container

Please note that I deleted few columns in the output above (Created and Ports) that are not needed in this case.

If I want to copy a file that is on my filesystem to that docker container all I have to do is to run the docker cp command that will copy from/to filesystem/container

Let’s see how it works

Copy from filesystem to container

~$ mkdir ~/test-copy
~$ cd ~/test-copy
~$ touch copy-a
~$ # now I want to copy this file to the container 5bdfd620b191
~$ # all I have to do is docker cp copy-a 5bdfd620b191:/path
~$ docker cp copy-a 5bdfd620b191:/home
~$ # at this stage I will execute the shell of the container
~$ docker exec -it 5bdfd620b191 /bin/sh
/ # ls
bin    etc    lib    mnt    proc   run    srv    tmp    var
dev    home   media  opt    root   sbin   sys    usr
/ # ls /home/
copy-a

Super handy!

I can also reverse this action, and by that I mean copy from the container to the filesystem. Let’s assume that in my container this file exists /home/copy-b all I have to do is

~$ docker cp 5bdfd620b191:/home/copy-b .

And the result of this action will be…I have now copy-b file on my filesystem.

Hope it will help

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %
Previous post How to show all the environment variables
Next post How to recover a lost MySQL root password
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x