Docker

Copy a Folder in Docker

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] CONTAINER:SRC_PATH DEST_PATH|-
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:

$ docker run -itd --rm busybox

 
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:

$ docker cp hello_world/ jovial_kalam:/home/busybox/hello_world

 
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:

$ docker cp container_name:/path/to/source_folder/ /path/to/destination/

 
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:

docker cp * container_name:/path/to/destination/

 
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:

$ docker cp lunar_zero:/path/to/source_folder/ /path/to/destination/

 
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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list