Docker

What is the Usage of the Docker Copy Command?

The “docker cp” command is an important command in Docker that is used for copying and transferring files and directories between a container and the host machine. It requires the container to be running to copy files to or from it. Also, the source and host paths must be absolute paths, and the destination path in the container must already exist.

This article will explain the following content:

What is the Usage of the Docker Copy Command?

The “docker cp” command can be used to back up data, restore data, or copy configuration files. The syntax of the docker cp command is as follows:

docker cp <container_name>:<src_path> <host_path>

This command copies the file or directory at the “<src_path>” location in the specified <container_name> to the “<host_path>” location on the host system. Users can also use this command in the opposite direction, copying directories and files from the host system to a running container. To do this, simply reverse the source and destination parameters.

Method 1: Copy a File/Directory from Container to the Local Host Machine

To copy a specific file/directory from the container to the local system, try out the below-mentioned steps:

  • List all existing containers.
  • Choose a certain container.
  • Copy a specific directory or file from the Docker container to the host machine through the “docker cp <container-name/id>:<directory/file-path> <host-machine-path>” command.
  • Verify the copied file.

Step 1: List All Existing Containers

First, display all the available containers and select the specific container to copy its file or directory:

docker ps -a

The above output showed all the existing containers. We have selected the “html-cont” container.

Step 2: Copy File/Directory from Docker Container to Local System

For copying a particular file/directory from the Docker container to the local system, write out the “docker cp <container-name/id>:<directory/file-path> <host-machine-path>” command:

docker cp html-cont:/usr/new.html C:\Docker\Data

Here:

  • html-cont” is the container name.
  • /usr/new.html” is the container’s file path.
  • C:\Docker\Data” is the directory path on the host machine:

This command has copied the “new.html” file from the container and saved it to the host machine.

Step 3: Verify the Copied File

Now, redirect to the host machine’s directory to verify whether the selected file has been copied into it or not:

cd C:\Docker\Data

Next, use the “ls” command and display the directory content:

ls

The output indicates that the “new.html” file has been copied successfully from a certain container to the host system.

Method 2: Copy a File/Directory from the Local Host Machine to Container

To copy a specific directory or a file from the host machine to a particular container, follow the provided steps:

  • Redirect to the local host directory.
  • List local host directory content.
  • Choose a particular directory or file.
  • Copy a selected directory or file from the host machine to the particular container using the “docker cp <host-machine-path> <container-name/id>:<directory/file-path>” command.
  • Verify copied file.

Step 1: Navigate to Local Host Directory

First, utilize the “cd” command along with the local machine’s directory path and navigate to it:

cd C:\Docker\Data

The output confirms the specified local host directory has been accessed.

Step 2: View Local Host Directory Content

Then, list the local host directory content and choose the desired file that needs to be copied to the Docker container:

ls

In the above output, two files can be seen. We have selected the “test.txt” file.

Step 3: Copy a File from Host Machine to Container

To copy a selected file from the host machine to the particular container, use the “docker cp <host-machine-path> <container-name/id>:<directory/file-path>” command:

docker cp C:\Docker\Data\test.txt html-cont:/usr

Here:

  • C:\Docker\Data\test.txt” is the file path on the host machine:
  • html-cont” is the container name.
  • /usr” is the container’s path:

This command has copied the “test.txt” file from the local host machine to the “html-cont” container.

Step 4: Verify Copied File

Now, verify whether the file has been copied to the container or not by accessing its content using the provided command:

docker exec -it html-cont sh

Note: This command works only if the container is running.

The above-stated command has opened the interactive shell. Now, we run commands in it to view the container’s content.

Navigate to the container directory using the “cd” command along with the directory name:

cd usr

Then, list the directory content:

ls

The above output shows that the “test.txt” file has been copied/transferred to the specified container successfully.

Conclusion

The “docker cp” command is utilized to copy or transfer files and directories between Docker containers and the local system. To copy or transfer a specific file/directory from the Docker container to the local system, use the “docker cp <container-name/id>:<directory/file-path> <host-machine-path>” command. For copying a selected file from the host machine to the container, the “docker cp <host-machine-path> <container-name/id>:<directory/file-path>” command is used.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.