Ender
Ender

Reputation: 1778

How to place files on shared volume from within Dockerfile?

I have a Dockerfile which builds an image that provides for me a complicated tool-chain environment to compile a project on a mounted volume from the host machines file system. Another reason is that I don't have a lot of space on the image.

The Dockerfile builds my tool-chain in the OS image, and then prepares the source by downloading packages to be placed on the hosts shared volume. And normally from there I'd then log into the image and execute commands to build. And this is the problem. I can download the source in the Dockerfile, but how then would I get it to the shared volume.

Basically I have ...

ADD http://.../file mydir
VOLUME /home/me/mydir

But then of course, I get the error 'cannot mount volume over existing file ..."

Am I going about this wrong?

Upvotes: 2

Views: 632

Answers (1)

larsks
larsks

Reputation: 312332

You're going about it wrong, but you already suspected that.

If you want the source files to reside on the host filesystem, get rid of the VOLUME directive in your Dockerfile, and don't try to download the source files at build time. This is something you want to do at run time. You probably want to provision your image with a pair of scripts:

  • One that downloads the files to a specific location, say /build.

  • Another that actually runs the build process.

With these in place, you could first download the source files to a location on the host filesystem, as in:

docker run -v /path/on/my/host:/build myimage fetch-sources

And then you can build them by running:

docker run -v /path/on/my/host:/build myimage build-sources

With this model:

  • You're trying to muck about with volumes during the image build process. This is almost never what you want, since data stored in a volume is explicitly excluded from the image, and the build process doesn't permit you to conveniently mount host directories inside the container.

  • You are able to download the files into a persistent location on the host, where they will be available to you for editing, or re-building, or whatever.

  • You can run the build process multiple times without needing to re-download the source files every time.

I think this will do pretty much what you want, but if it doesn't meet your needs, or if something is unclear, let me know.

Upvotes: 5

Related Questions