Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker build using volumes at build time

Is there a way to use external volumes during the docker image build ?

I have a situation where I would like to use a configuration inside a external volume during the docker image build time. Is that possible?

like image 816
Varunkumar Manohar Avatar asked Jun 28 '18 15:06

Varunkumar Manohar


2 Answers

(edited to reflect current Docker CLI behavior)

If by 'docker image build' you mean running a single 'docker build ...' command: no, there is no way to do that (at least, not in the most recent documentation that I have read). However, nothing prevents you from performing the step that needs the external volume using direct docker commands and then commit the container and tag it just as 'docker build' would. Assuming this is the last step in your build, put all other commands (that don't need the volume) into a Dockerfile and then do this:

tmp_img=`docker build .`
tmp_container=`docker run -d -v $my_ext_volume:$my_mount_path --entrypoint=(your volume-dependent build command here) $tmp_img`
docker wait "$tmp_container"
docker commit $tmp_container my_repo/image_tag:latest
docker rm "$tmp_container"

This does the same as having a RUN command in the Dockerfile, but with the added volume mount. The commit command in the example also tags the image.

It is a bit more complex if you need to have other Dockerfile commands after the volume-dependent one, but in most cases you can combine run commands and re-arrange your install in a way that leaves the manual run-with-volume command last, to keep things simple.

like image 174
Leo K Avatar answered Nov 04 '22 12:11

Leo K


You can copy the file into the docker image (ADD) and rm as one of the last steps

like image 40
Arnon Rotem-Gal-Oz Avatar answered Nov 04 '22 13:11

Arnon Rotem-Gal-Oz