Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a file from container to host during build process

How do I copy files from a docker container to the host machine during docker build command?

As a part of building my docker image for my app, I'm running some tests inside it, and I would like to copy the output of the test run into the host (which is a continuous integration server) to do some reporting.

like image 896
govin Avatar asked Nov 24 '14 20:11

govin


People also ask

Can I copy a file from docker container to host?

Docker to host file copy commandAfter you specify the container name or ID, simply provide the full path to the file you want to copy, and then provide the name of the destination folder on the host system.

How do I copy a file out of container?

You can use the docker cp command to copy the file. The first path (Source) is the path in the Docker Container and the second one is the path inside your Local System (Destination).

How do I transfer files from container to local machine?

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH to copy from the container to the host machine. For Vice-Versa: docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH to copy from host machine to container.


2 Answers

I wouldn't run the tests during build, this will only increase the size of your image. I would recommend you to build the image and then run it mounting a host volume into the container and changing the working directory to the mount point.

docker run -v `pwd`/results:/results -w /results -t IMAGE test_script
like image 163
Javier Castellanos Avatar answered Dec 01 '22 11:12

Javier Castellanos


There is no easy way to do this. Volumes are only created at run time. You can grab it out of the docker filesystem, (e.g. mine is /var/lib/docker/devicemapper/mnt/CONTAINER_ID/rootfs/PATH_TO_FILE) though there is no good way to figure out when your test process is complete. You could create a file when it's finished and do an inotify, but this is ugly.

like image 35
seanmcl Avatar answered Dec 01 '22 11:12

seanmcl