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.

Thanks

Aug 27, 2018 in Docker by Damon Salvatore
• 5,510 points
1,988 views

1 answer 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       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,510 points
0 votes
  • 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 by Sirajul
• 19,910 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,100 points

edited Oct 16, 2018 by lina 3,844 views
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 in Docker by Ashish
2,497 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
• 9,530 points
304 views
0 votes
1 answer
0 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
• 56,520 points
136 views
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 in Docker by Sirajul
• 19,910 points
76 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 in Docker by Sirajul
• 19,910 points
828 views