Docker

How to Use the “docker copy” Command to Transfer a File from a Docker Container to the Host Machine?

Docker containers are lightweight and isolated environments that execute an application using a docker image. The files in a docker container are stored in a layered filesystem, which consists of read-only layers from the image. While working on Docker, users may need to transfer a particular file of the container to the host machine for various reasons, such as sharing it with other team members, backing up data, debugging, etc.

This article will explain the step-by-step procedure to transfer a file from the Docker container to the local host machine.

How to Use the “docker copy” Command to Transfer a Specific File from a Docker Container to the Local Host Machine?

To transfer a specific file from the Docker container to the local host system, try out the below-listed steps:

  • List all Docker containers.
  • Select the particular container.
  • Transfer the desired file from the container to the local host machine using the “docker cp <container-name/id>:<file-path> <host-machine-path>” command.
  • Verify the Transferred File.

Step 1: Display All Existing Containers
First, list all the available containers and choose a particular container to copy its file:

docker ps -a

The output has displayed all the containers. We choose the “html-cont” container for further steps.

Step 2: Transfer File from Docker Container to Host Machine
To transfer a file from the Docker container to the host machine, use the “docker cp <container-name/id>:<file-path> <host-machine-path>” command:

docker cp html-cont:/usr/share/nginx/html/index.html C:\Docker\HTML

Here:

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

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

Step 3: Verify the Transferred File
Redirect to the host machine’s directory to verify whether the desired file has been transferred into it or not:

cd C:\Docker\HTML

Then, display the content of the host machine’s directory using the provided command:

ls

The output indicates that the “index.html” file has been transferred successfully from the Docker container to the host machine.

Alternatively, users can also navigate to the desired directory on PC to view the transferred file:

We have successfully transferred the “index.html” file from the Docker container to the host machine using the “docker cp” command.

Conclusion

To transfer a desired file from a particular Docker container to the local host system, first, select the desired Docker container to copy its file. Then, run the “docker cp <container-name/id>:<file-path> <host-machine-path>” command to transfer the desired file from the container to the host machine. Finally, redirect to the host machine’s directory and view its content for verification. This article has explained the method to transfer a file from the Docker container to the local host machine.

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.