File operation is the most basic and yet fundamental operation in computing. It is no doubt that Docker has revolutionized the way we build, ship, and run the applications by providing a method of enclosing them into portable, standalone units.
As you can guess, one of the common tasks when it comes to Docker is transferring the files between the host machine and a Docker container.
In this tutorial, we will show you how to copy the files and directories between the host machine and a Docker container. We will discuss the methods that work on both the running and stopped container.
Docker CP Command
The Docker ācpā utility allows us to copy the files and directories of the specified source path to the destination path.
The command syntax is as follows:
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
The ācpā command can also allow you to stream a tar archive from standard in or standard out.
It is good to keep in mind that the command assumes that the container paths are relative to the container’s root directory.Ā Hence, supplying the initial forward slash is optional as the command prepends it by default.
Run the Basic Container
For demonstration purposes, let us start by running a basic container that we will use to copy the files and directories.
For this tutorial, we use the BusyBox image which provides a minimalistic lightweight Linux image that provides basic Unix utilities.
Run the command as follows:
This should run a new Docker container that uses the BusyBox image.
Copy a Folder from the Host to the Container
The first and most common operation between the Docker container and the host machine is copying a directory from the host machine to a container.
Suppose we have a directory called āhello_worldā in the host machine and we wish to copy it into a running container called ājovial_kalamā. We can use the ādocker cpā command as follows:
In the given example, we specify the source directory on the local machine as āhello_worldā. We then specify the name or ID of the target container on to which we wish to copy the directory.
Finally, we specify the destination path inside the container.
Copy a Folder from the Container to the Host
As you can guess, we can also retrieve a directory from a running container and save it into the host machine using the command as follows:
In this case, the command should download the specified directory from the running container to the host machine.
Copy Multiple Files
To copy multiple files from the host machine to a Docker container, we can use a wildcard character like * as shown in the following example:
This should copy all the files in the specified directory to the host machine.
Copy from a Stopped Container
Docker does allow us to copy the files and directories from a stopped container. For example, suppose we have a stopped container called ālunar_zeroā and we want to copy a directory from it. We can run the command as follows:
Replace the ālunar_zeroā entry with the name of the container from which you wish to copy the specified directory.
Conclusion
In this tutorial, we learned how to work with the Docker ācpā utility to copy the files and directories to and from a Docker container. We also covered how you can access the filesystem of a container with the ācpā command whether the container is running or stopped.