1. Overview
This quick tutorial will focus on how to copy files, folders from host to Docker container and vice versa by using the docker cp command.
2. The docker cp command
The docker cp command which allows us to copy files/folders between a container and the local filesystem, has the syntax as the following:
1 2 |
docker cp <span class="o"><span class="hljs-string">[</span></span><span class="hljs-string">OPTIONS]</span> CONTAINER:SRC_PATH DEST_PATH|- docker cp <span class="o"><span class="hljs-string">[</span></span><span class="hljs-string">OPTIONS]</span> SRC_PATH|- CONTAINER:DEST_PATH |
Where OPTIONS can be:
Name, shorthand | Default | Description |
---|---|---|
--follow-link, -L |
false |
Always follow symbol link in SRC_PATH |
Note that to execute the command, we need to know exactly the container id, name on which we want to perform action. The command to list all available container is:
1 |
docker ps |
You can refer to my recent post to learn more about Docker commands.
3. Copy Files, Folders from Host to Docker Container
Let’s see several examples that we copy a file from host to a Docker container:
1 |
docker cp abc.txt my-alpine:/ |
The above command copy the file abc.txt to the / directory in the my-alpine container.
Let’s see another example which we copy the file abc.txt to the /tmp folder in the my-alpine container and change the file name to bcd.txt.
1 |
docker cp abc.txt my-alpine:/tmp/bcd.txt |
4. Copy Files, Folders from Docker Container to Host
Let’s see the following example which we copy the file /root/data/xyz.txt from the my-alpine Docker container to the /tmp/ folder on the host machine:
1 |
docker cp my-alpine:/root/data/xyz.txt /tmp/xyz.txt |
Another example which copy the same file from container to the current directory on the host and then change the file name:
1 |
docker cp my-alpine:/root/data/xyz.txt xyzw.txt |
5. Conclusion
The quick tutorial has just illustrated us how to copy files, folders from host to Docker container and vice versa by using the docker cp command. Currently, there is no direct way to copy files, folders between containers, however we can copy data from containers to host machine a folder on host machine temporarily then copy them to other containers. Another approach related to shared volume will be shared on a next tutorial.
Below are other related tutorial for your references:
Copy Files, Folders from Host to Docker Container and Vice Versa
How to Get IP Address of a Docker Container
How To Pull A Docker Image And Run A Container
Install Docker on Ubuntu 16.04, 15.10, 14.04 Step By Step
Introduction to Docker Compose
Vagrant Docker Provider Tutorial