The way to copy files between a Unix host system and a Docker container is to use the docker cp
command.
The syntax of this command is close to the syntax of the Unix cp
command:
docker cp <SRC> <DEST>
In this example, we copy a file from the /tmp directory on the host machine into the /tmp directory in the ContExample container:
docker cp /tmp/example.txt ContExample:/tmp/
Lets consider "13ff526fdb91" the ID of the ContExample container. We can also use the ID of the container istead of its name:
docker cp /tmp/example.txt 13ff526fdb91:/tmp/
If you want to copy a file from the container to the host machine, just switch the order of the parameters from the previous example:
docker cp ContExample:/tmp/example.txt /tmp/
Finally, you can copy an entire directory, not just single files.
For example, if you want to copy the entire ExampleDir from the ContExample container to the host machine, do it like this:
docker cp ContExample:/tmp/ExampleDir /tmp/
Some of the limitations of docker cp
command are:
docker cp
command to copy between two containers. It can be used only to copy between a single container and the hosting machine.cp
command, it doesn't support the same flags. The docker cp
command only supports two flags:
--archive , -a |
Archive mode (copy all uid/gid information) |
x--follow-link , -L |
Always follow symbol link in SRC_PATH |