Docker Community Forums

Share and learn in the Docker community.

Mounting volume during multi-stage build

I am building application inside maven container so programmer doesn’t need to worry about version of java and maven on local machine. But i have problem with .m2, every time application is build it is downloading all dependencies from maven. Is there any way how to mount m2 to maven container during multi stage build?

Here is simplified Dockerfile for demonstration.

FROM maven:3.5.2-jdk-9 AS base_build
COPY . /tmp/app
WORKDIR /tmp/app\
RUN mvn clean package

FROM jboss/wildfly/:11.0.0.Final
COPY --from=base_build ./tmp/app/application/ear/target/cmc-ear-*.ear /opt/jboss/wildfly/standalone/deployments/

the source of mounts are required to be in the SAME directory tree as the dockerfile…

BUT you could make a symbolic link to it, and mount THAT…

note that .m2 needs to be in the user directory (root by default) when u mount it…

but then u would have a container modifying the hosts maven cache…

maybe map an empty folder as the .m2 folder in the container, and reuse that


docker run -d … -v ./build_m2:/root/.m2 imagename

then it can reuse the build only .m2 (next time it is there)

Did you find a solution to this?

@sdetweil not sure what you mean by “maybe map an empty folder as the .m2 folder in the container”?

My problem is the volume is not available to build instruction RUN mvn clean package

Hello. Please share your Dockerfile if you found a solution.