How do I copy a file from docker container to host

+1 vote

I don't want to install all the runtimes and libraries on the agents themselves so I am thinking to use docker to build my dependencies on a continuous integration server. To achieve this I would need to copy the build artifacts that are built inside the container back into the host.

How do I do that?

Can anyone help me with this.

Thanks

Aug 27, 2018 in Docker by Damon Salvatore
• 5,980 points
25,543 views

2 answers to this question.

+2 votes

Here is a way which you can try if you want to copy a file from a container to the host. you can use the below command:

docker cp <containerId>:/file/path/within/container /host/path/target

Here's an example:

[sam@dev]$ sudo docker cp goofy_roentgen:/out_read.jpg .

Here goofy_roentgen is the name I got from the following command:

[sam@dev]$ sudo docker ps
[sudo] password for jalal:
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                            NAMES
1b4ad9311e93        bamos/openface      "/bin/bash"         33 minutes ago      Up 33 minutes       0.0.0.0:8000->8000/tcp, 0.0.0.0:9000->9000/tcp   goofy_roentge


I hope it will resolve your query.

answered Aug 27, 2018 by Damon Salvatore
• 5,980 points
How to copy file from container to host using docker-compose.yaml file. Would you please help me in this

Hi@Saurav,

You can use the volume parameter in your Docker compose script as shown below.

volumes:
      - /dir/on/host:/var/www/html

It will sync your file form the local system to the container.

After running the mentioend command md5sum is getting changed with the copied file in host, so how to maintain the checksum after copying the file on host

Hi@Debashis,

You are trying to copy files from the docker container to the host right. So simply use the docker cp command. But how the checksum is getting changed and why it is required. Can you explain your query a little bit more?

+1 vote
  • Instead of using docker run you could achieve this using docker create. 

  • The docker create command creates a writeable container layer over the specified image and prepares it for running the specified command. The container ID is then printed to STDOUT. 

  • This is similar to docker run -d except the container is never started.

  • Try this, you never start the container here:

docker create -ti --name dummy IMAGE_NAME bash
docker cp dummy:/path/to/file /dest/to/file
docker rm -fv dummy
answered Aug 13, 2019 by Sirajul
• 59,090 points

Related Questions In Docker

+4 votes
4 answers

How To Access a Service on Host From a Docker Container?

Adding to kalgi's answer, You can also ...READ MORE

answered Oct 16, 2018 in Docker by lina
• 8,220 points

edited Oct 16, 2018 by lina 23,166 views
+1 vote
2 answers
0 votes
2 answers

How to edit file after I shell to a docker container?

You can even install it using a ...READ MORE

answered Apr 23, 2019 in Docker by Ashish
15,516 views
0 votes
1 answer

How to run a docker command from inside the container?

You must have come across the /var/run/docker.sock file, ...READ MORE

answered Jun 28, 2018 in Docker by Sophie may
• 10,550 points
2,020 views
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,120 points
1,233 views
+1 vote
2 answers

How do I run a docker image as a container?

You can run an image depends on whether you ...READ MORE

answered Sep 7, 2018 in Docker by Damon Salvatore
• 5,980 points
493 views
0 votes
2 answers

How do I force Docker for a clean build of an image ?

You could try this inorder to clean ...READ MORE

answered Aug 6, 2019 in Docker by Sirajul
• 59,090 points
7,503 views