How to copy files from host to Docker container

How to copy files from host to Docker container

Views: 35
0 0
Read Time:1 Minute, 24 Second

This is a small how to for helping you can copy files from host to Docker 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

fce70581d3812712df039a05e6d32a7d How to copy files from host to Docker container

About Post Author

brewedbrilliance.net

Experienced software architect with a spasmodic passion for tech, software, programming. With more than 20years of experience I've decided to share all my knowledge in pills through this blog. Please feel free to contact me if there is any topic that you would love to cover
happy How to copy files from host to Docker container
Happy
0 %
sad How to copy files from host to Docker container
Sad
0 %
excited How to copy files from host to Docker container
Excited
0 %
sleepy How to copy files from host to Docker container
Sleepy
0 %
angry How to copy files from host to Docker container
Angry
0 %
surprise How to copy files from host to Docker container
Surprise
0 %

Share this content:

Experienced software architect with a spasmodic passion for tech, software, programming. With more than 20years of experience I've decided to share all my knowledge in pills through this blog. Please feel free to contact me if there is any topic that you would love to cover

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x