How to Use docker cp to Copy a File from a Docker Container to Host

Eyecatch

Introduction

This article describes how to copy files and directories from the Docker container to the host, and vice versa.

These operations were complicated in older versions of Docker, but are now easily accomplished by using the docker cp command.

After explaining docker cp below, I will explain how to use the command with actual examples.

What is docker cp command?

The docker cp command is a command that allows you to transfer files and directories between the Docker host and the container in the same format as the Unix cp command.

Specifically, it has the following two functions.

  • Copy files and directories on the Docker host into the container
  • Copy files and directories in the container onto the Docker host

Let's look at a concrete example below.

Copy files from container to host

Basic usage when copying files from a container to a host

The basic syntax for copying files from a container to a host using the docker cp command is as follows: You can easily copy by just specifying the file path and output destination in the container you want to copy.

$ docker cp [OPTIONS] CONTAINER: SRC_PATH DEST_PATH

Specific examples are as follows. This example copies the files in the path / dir / file in the container testvm to the current directory on the Docker host.

$ docker cp testvm: / dir / file.

You can also copy with another name by specifying the file name as shown below. In the following example, the file in the path / dir / file in the container named testvm is renamed and copied to the current directory of the Docker host as file2.

$ docker cp testvm: / dir / file file2

Copy directory into container (recursive copy)

The same is true when copying a directory from a Docker container to a host instead of a file. Specify the directory, not the file, in the command. The following copies the dir directory in the testvm container to the current directory on the Docker host.

$ docker cp testvm: / dir.

Note that this is different from the Unix cp command. The docker cp command differs from the Unix cp command in that when copying directories, recursive file copying is done by default. In other words, not only the directory but the entire contents are copied.

Copy file from host to container

So what if you want to copy files from a Docker host to a container? In that case, specify the reverse of the specification when copying from the Docker container to the host. (It's easy!) The basic syntax for copying files from a host to a Docker container using the docker cp command is as follows.

docker cp [OPTIONS] SRC_PATH CONTAINER: DEST_PATH

Specific examples are as follows. In this example, the file on the host is copied to the directory/ dir /in the container named testvm.

$ docker cp file testvm: / dir /

If you want to change the file name, it is the same as the previous example. It can be changed by specifying the file name in the specification of the copy destination path. In this case, I renamed it from file to file2 and copied it to / dir in the testvm container.

$ docker cp file testvm: / dir / file2

Copy directory into host (recursive copy)

When copying a directory, specify the directory name as you would a file. The following is an example of copying the dir directory to the testvm container.

$ docker cp ./dir testvm: / dir

Again, the files in the directory are recursively copied from the host as well as the container.

Tips: behavior of docker cp

Finally, I will show you the specifications for mastering the docker cp command.

As mentioned above, using docker cp is similar to the general Unix utility cp. Specifically, it behaves as follows.

  • You can copy files even if the container is stopped.
  • If possible, recursively copy the directory along with the permissions.
  • File owner permissions specify the user and primary group at the end of the transfer. For example, if you copy the file to a container, the UID: GID will be created as the root user.
  • On the contrary, when copying the file to the local machine, the UID: GID of the user who executed the docker cp command is created.

Conclusion

I shown you how to copy files from a Docker host to a container and how to copy files from a Docker container to a host.

The docker cp command to achieve these points has much in common with the cp command, so it may be easier to operate intuitively. See the Docker documentation below for detailed command options and usage. https://docs.docker.com/engine/reference/commandline/cp/

Logo

Secure your IaC with just a few clicks!

You can keep your IaC security for free. No credit card required.

Share This Post