Docker: Copy files from Docker container to local host

Docker: Copy files from Docker container to local host

This tutorial guides you on how to copy files from Docker container to local host. Let’s see how to use docker cp command to copy files from docker container to host with some examples.

Docker: Copy files from Docker container to local host

Docker: Copy files from Docker container to local host

Before you begin make sure that you had installed Docker on your local machine. For example, I have installed docker and pulled mysql image from docker hub and running as a container as shown below.

C:\Users\temp> docker ps -a

CONTAINER ID   IMAGE                    COMMAND                  CREATED       STATUS                     PORTS                                       NAMES
cbc9f84f255c   mysql:latest            "docker-entrypoint.s…"   4 weeks ago   Up 4 days                  3306/tcp, 33060/tcp                         mysql

Let’s say you wanted to copy some specific file from root directory from your mysql docker container. For instance, lets try to copy the file initdb.sql. Note, you need to execute docker exec bash command to get to the file system of your docker container as shown below.

C:\Users\temp> docker exec -it mysql bash -l

root@cbc9f84f255c:/# ls
bin   dev                         entrypoint.sh  home        lib    media  opt   root  sbin  sys  usr
boot  docker-entrypoint-initdb.d  etc            initdb.sql  lib64  mnt    proc  run   srv   tmp  var

Once you make sure that the file that you wanted to copy exists in the docker container, you can exit.

root@cbc9f84f255c:/# exit

logout

Next step is important as you are going to copy the required file from docker container to your host using docker cp command.

> docker cp mysql:/initdb.sql C:\Users3248272\temp

Verify the file copied from docker container

Finally verify whether the file is copied to your local host temp directory.

C:\Users\temp> dir

 Volume in drive C is OS
 Volume Serial Number is C25A-D628

 Directory of C:\Users3248272\temp

27-07-2021  19:42    <DIR>          .
27-07-2021  19:42    <DIR>          ..
29-06-2021  15:12            23,050 initdb.sql
               1 File(s)         23,050 bytes
               2 Dir(s)  57,866,977,280 bytes free

That’s it you could see that initdb.sql file is copied from docker container to your host. Make sure that you check the content of the files too.

Hope it is helpful 🙂

References

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments