How to mount volume created during build?

Is it possible to mount a volume during docker build, with the VOLUME dockerfile command?

I am using Docker for Mac and want to be able to mount that volume on my host system, so that I can access those files on my host files system.

1 Like

ADD / COPY within the Dockerfile also could access your host system. They access the place where you invoked the docker build command.
@saulshanabrook: Isn’t that sufficient for your usecase?

I am using this workflow to mount my source code after a npm install. I then want to extend it and mount the node_modules that were installed in the docker image, back in my host system, so that my intellisense editor can pick up the installed files.

1 Like

i want to do a very similar thing as @saulshanabrook, only with java/maven

in the docker file i am doing a RUN mvn clean compile
which in turn ALWAYS pulls down all the dependencies for my project.
i really would love to see a build time volume mount, where i could mount in my host machine’s ~/.m2 folder to speed up the image build.

my ~/.m2 folder currently is about 10GB big, so ADD/COPY -ing it to the image is not an option.

Hi I want to do the same with my maven projects. Is there any update on this?

Here’s a sample of my Dockerfile that I use for Maven (note not all dependencies can be computed so you may have to add those dependencies explicitly into your cache.

FROM maven
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src/ src/
RUN mvn -q package

FROM openjdk:11-jre
COPY --from=0 target/unix-domain-socket-1.0-SNAPSHOT.jar /ms.jar
CMD java -jar /ms.jar

The intermediate image will be large but that’s not where the final image goes to.

In terms of your CI/CD pipeline, you can “cache” the last build result using load and save so that it won’t download unless needed.

You can also keep a nexus server close to your build machine so it goes faster.