How do I copy a file from docker container to host?

0 votes

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.


Aug 27, 2018 in Docker by Damon Salvatore
• 5,550 points

2 answers to this question.

+1 vote

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>8000/tcp,>9000/tcp   goofy_roentge

I hope it will resolve your query.

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


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

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

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

+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
• 58,130 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 16,817 views
0 votes
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
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,100 points
+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
• 57,700 points
0 votes
2 answers

How do I run a docker image as a container?

You could probably follow these steps : $ docker ...READ MORE

answered Aug 9, 2019 in Docker by Sirajul
• 58,130 points
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
• 58,130 points