11

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.

4
  • Why not just run the tests after the build? Commented Nov 24, 2014 at 23:35
  • Why would I build it if the tests are not passing? Commented Sep 13, 2017 at 14:01
  • How to copy files from dockerfile to host? Commented Nov 17, 2017 at 6:17
  • 2
    Did you find a solution for your problem? Commented Mar 20, 2018 at 20:12

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.