在构建阶段将.m2文件绑定到docker [英] Bind .m2 file to docker on build stage
问题描述
我试图根据下面的docker文件在docker容器中构建一个spring boot项目,但是每次都从互联网上下载所有mvn依赖项.构建docker文件时如何绑定本地.m2文件.
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.
这是我的 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"]
推荐答案
您应将项目内容装入docker映像中,并将 $ HOME/.m2/
装入映像中,而不要复制一切都放入图像中并建立新图像.
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..
$ PWD
是您的 pom.xml
文件所在的本地目录,并且 src
目录存在...
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
- 定义
pom.xml
所在的工作目录的位置. - 定义本地缓存的位置.
- 定义目标目录以将其映射到给定路径下的图像中
- 定义工作目录.
- 定义要使用的图像的名称.
- defines the location of your working directory where
pom.xml
is located. - defines the location where you have located your local cache.
- defines the target directory to map it into the image under the given path
- defines the working directory.
- defines the name of the image to be used.
因此,您无需创建新图像即可使用Maven构建自己的东西.只需通过以下命令运行现有映像:
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
这篇关于在构建阶段将.m2文件绑定到docker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!