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:
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:
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:
Then, display the content of the host machine’s directory using the provided command:
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.