Bind .m2 file to docker on build stage

孤街浪徒 提交于 2021-02-10 20:49:01

问题


I tried build a spring boot project in docker container based on below docker file.But every times all mvn dependency download from internet. How can I bind local .m2 file when i build the docker file.

This is my Dockerfile

FROM maven:3.5-jdk-8-alpine AS build 
COPY /src /usr/src/javaspring/src
COPY pom.xml /usr/src/javaspring
COPY Dockerfile /usr/src/javaspring
RUN mvn -f /usr/src/javaspring/pom.xml clean install


FROM openjdk:8-jre-alpine
COPY --from=build /usr/src/javaspring/target/javaspring-1.0.jar app.jar
ENTRYPOINT [“java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

回答1:


You should mount the content of your project into the docker image and the $HOME/.m2/ into the image instead of copying everything into the image and building a new image..

The $PWD is the local directory where your pom.xml file is located and the src directory exists...

docker run -it --rm \
  -v "$PWD":/usr/src/mymaven \ (1)
  -v "$HOME/.m2":/root/.m2 \ (2)
  -v "$PWD/target:/usr/src/mymaven/target" \ (3)
  -w /usr/src/mymaven \ (4)
  maven:3.5-jdk-8-alpine \ (5)
  mvn clean package
  1. defines the location of your working directory where pom.xml is located.
  2. defines the location where you have located your local cache.
  3. defines the target directory to map it into the image under the given path
  4. defines the working directory.
  5. defines the name of the image to be used.

So you don't need to create an new image to build your stuff with Maven. Simply run an existing image via the following command:

docker run -it --rm \
  -v "$PWD":/usr/src/mymaven \
  -v "$HOME/.m2":/root/.m2 \
  -v "$PWD/target:/usr/src/mymaven/target" \ 
  -w /usr/src/mymaven \
  maven:3.5-jdk-8-alpine mvn clean package


来源:https://stackoverflow.com/questions/52015939/bind-m2-file-to-docker-on-build-stage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!