Docker: Cp Command – Copy File ( To | From ) Container

The docker cp command serves for copying files and folders between Docker container and a host machine.

It is possible to copy a file or folder from host to container as well as from container to host.

In the examples below i am coping the files but the syntax for copying folders is the same.

Cool Tip: How do i list ( running | stopped | all ) Docker containers! Read More →

Docker Cp Command

Find out a container’s name or ID using the docker ps command:

$ docker ps
72ca2488b353  my_image          X hours ago  Up X hours         my_container

Copy a file from host to container:

$ docker cp foo.txt 72ca2488b353:/foo.txt

Copy a file from Docker container to host:

$ docker cp 72ca2488b353:/foo.txt foo.txt
Was it useful? Share this post with the world!

11 Replies to “Docker: Cp Command – Copy File ( To | From ) Container”

  1. Thanks for the info, using this command, I am able to copy from container to my host

  2. ihsanberahim says: Reply


  3. Hélio Rugani Brandão says: Reply

    Good and practical info. This simple way as the example of the command was written is clearer than docker’s manual which one lacks clarity some times.

  4. It’s g, Thank you, it helps me a lot!

  5. This is helpful, however how do I then access this file once it is on the container. I am attempting to bulk insert into a mssql server, but seem to be inserting nothing.

  6. Hi. Can I copy multiple files though?

  7. How do u use a dynamic id ? what if u want to use a script to make the copy? u need to set the id every time because it changes..

    1. Mohammad Shawer Khandariya says: Reply

      You can use container_name instead of container_id for ex: docker cp foo.txt container_name:/foo.txt

  8. Hi. Question: where would the foo.txt go after being copied from the docker container? What is it’s path?

  9. docker cp 72ca2488b353:/foo.txt foo.txt
    taking the command above as an example, it will be your current directory where you execute the command. Unless you specify the path . eg: docker cp 72ca2488b353:/foo.txt ~/Desktop/foo.txt
    then it will be located under Desktop folder

  10. Mohammad Shawer Khandariya says: Reply

    You can access the file inside docker using docker exec -ti container_name sh command

Leave a Reply