Docker

Copy a File from the Parent Directory to the Docker Container

File transfer is a common task, especially when working with multiple systems. In the context of Docker, you will often need to transfer the files between the host machine and a Docker container.

In this fundamental tutorial, we will explore how we can copy the files from a host to a Docker container using the “cp” command.

Requirements:

In order to follow along with this tutorial, ensure that you have the following:

    1. A Linux host
    2. Installed Docker version 20.0 and above on the host
    3. Permissions to read and write the files on both the host and the container

Docker Cp Command

In Docker, the “cp” command allows us to copy the files and directories from the host system to a running container in simple steps.

The command follows a simple syntax as shown in the following:

$ docker cp src_path container_id:dest_path

 
The command takes the “src_path”, “container_id”, and “dest_path” parameters where the “src_path” represents the path on the host machine to the file or directory that you wish to copy to the container.

The container specifies the name or the ID of the container to which you wish to copy the files.

Finally, the “dest_path” parameter specifies the container’s path on which you wish to copy the files.

The command requires you to specify the ID of the container on which to copy the commands. You can get the ID of the container using the “docker ps” command.

Docker Cp Command Example

Let us take a practical example to learn how to use the “docker cp” command. We start by running a container using the Alpine image as shown in the following example command:

$ sudo docker run -it --rm alpine /bin/sh

 
Once we create the container, we can exit from the shell by running the “exit” command to drop back to the host shell.

Copy a Single File into the Container

To copy a single file into a container, we can use the relative or absolute paths. For example, suppose we wish to copy the file called “nginx.conf” from /etc/nginx to /opt/nginx of the container. We can run the following command:

$ sudo docker cp /etc/nginx/nginx.conf container:/opt/nginx/nginx.conf

 
You can copy multiple files at once using the “docker cp” command by taking advantage of the Bash features such as a “for” loop as follows:

for file in */etc/nginx/sites-enabled/*.conf; do sudo docker cp $file container:/opt/nginx/

 
This iterates the specified directory, locates the matching files, and transfers them to the container in the set destination.

Copy the Directory Recursively

The most efficient method of transferring multiple files from the host to a running container is by copying a directory recursively.

An example is as follows:

$ sudo docker cp /etc/nginx/sites-enables container:/opt/nginx/

 
It is good to remember that the “docker cp” command assumes that the container paths are relative to the container’s root directory.

Conclusion

In this post, we explored how to utilize the “docker cp” command in the Docker ecosystem to transfer a single or multiple files or directories from the host system into a running container.

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