This tutorial guides you on how to copy files from Docker container to local host. Let’s see how to use docker cp command to copy files from docker container to host with some examples.
Docker: Copy files from Docker container to local host
Before you begin make sure that you had installed Docker on your local machine. For example, I have installed docker and pulled mysql image from docker hub and running as a container as shown below.
C:\Users\temp> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cbc9f84f255c mysql:latest "docker-entrypoint.s…" 4 weeks ago Up 4 days 3306/tcp, 33060/tcp mysql
Let’s say you wanted to copy some specific file from root directory from your mysql docker container. For instance, lets try to copy the file initdb.sql. Note, you need to execute docker exec bash command to get to the file system of your docker container as shown below.
C:\Users\temp> docker exec -it mysql bash -l root@cbc9f84f255c:/# ls bin dev entrypoint.sh home lib media opt root sbin sys usr boot docker-entrypoint-initdb.d etc initdb.sql lib64 mnt proc run srv tmp var
Once you make sure that the file that you wanted to copy exists in the docker container, you can exit.
root@cbc9f84f255c:/# exit logout
Next step is important as you are going to copy the required file from docker container to your host using docker cp command.
> docker cp mysql:/initdb.sql C:\Users3248272\temp
Verify the file copied from docker container
Finally verify whether the file is copied to your local host temp directory.
C:\Users\temp> dir Volume in drive C is OS Volume Serial Number is C25A-D628 Directory of C:\Users3248272\temp 27-07-2021 19:42 <DIR> . 27-07-2021 19:42 <DIR> .. 29-06-2021 15:12 23,050 initdb.sql 1 File(s) 23,050 bytes 2 Dir(s) 57,866,977,280 bytes free
That’s it you could see that initdb.sql file is copied from docker container to your host. Make sure that you check the content of the files too.
Hope it is helpful 🙂
- How to Explore Docker Container’s File System ?
- Docker EXPOSE Port only to the Host on Google Cloud
- Get Docker Container’s IP Address from the Host
- Copy Files between Host and Docker Container
- Install Anthos Service Mesh (Istio) on GKE Kubernetes Cluster ?
- kubectl unable to connect to server: x509: certificate signed by unknown authority
- How to list all Containers running in Kubernetes Pod ?
- List all kafka topics using kafka testclient ?
- Google cloud shell command to get the default PROJECT_ID – GCP
- How to remove an image tag from docker hub ?
- Build a Docker Image with a Dockerfile and Cloud Build in GCP?
- How to create GCP project on Google Cloud Platform
- MariaDB – How to set max_connections permanently ?
- View Kafka messages using kafka testclient ?
- What is ImagePullBackOff status on a Kubernetes pod ?
- List all kafka topics using kafka testclient
- Delete docker repository from the docker hub
- Replication factor: 1 larger than available brokers: 0 – Create Kafka Topic
- Docker: Error response from daemon get https //registry-1.docker.io/v2/
- Generate merged code coverage report for JUnit and BDD tests in Sonar using Jacoco