How to copy files between a Unix host system and a running Docker container

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>

Examples

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/

Limitations

Some of the limitations of docker cp command are:

  1. We cannot use docker cp command to copy between two containers. It can be used only to copy between a single container and the hosting machine.
  2. Even tough it has the same syntax as the Unix 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

Other articles:

How to use bash Environment Variables in Python - part 1

How to use bash Environment Variables in Python - part 2

How to use bash Environment Variables in Python - part 3

How to plot multiple functions on the same figure using Matplotlib