背景:
Docker每次build都会重新生成一个镜像,导致每次执行maven构建会重复下载jar包,一般一个spring boot 单体应用光是Jar包都有50M起步,假设网络延迟比较大一个镜像打包起码要5分钟起步,于是经过实验总结出2套方案,可以很好的避免每次构建镜像重复下载Jar包问题
方案一
1.先新建一份DockerFile_Base 基础镜像
# 拉取编译环境 FROM maven:3.8.1 as builder # 将源码拷贝到固定目录 COPY . /repository # 切换到源码目录 WORKDIR /repository COPY settings.xml /root/.m2/settings.xml # 使用maven进行编译 RUN mvn clean package -DskipTests RUN rm -rf /repository
2.在服务器上执行build,让jar包提前下载到maven仓库
docker build -t='maven-base:latest.
3.依赖的Maven服务模块DockerFile脚本依赖Base镜像
# 拉取编译环境 FROM maven-base:latest as builder # 将源码拷贝到固定目录 COPY . /repository # 切换到源码目录 WORKDIR /repository COPY settings.xml /root/.m2/settings.xml # 使用maven进行编译 ARG EVN_NAME=dev RUN mvn clean package -pl consumer -am -amd -DskipTests -P ${EVN_NAME} # 拉取运行环境镜像 FROM openjdk:11.0.16 VOLUME /tmp COPY --from=builder /repository/consumer/target/app /app WORKDIR /app/bin RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone EXPOSE 8090 RUN rm -rf /repository ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","-Xms4g", "-Xmx4g", "-XX:+UseG1GC","xxx.jar"]
方案二
不依赖于方案一的Docker Base,通过-Dmaven.repo.local 指定Jar包路径
#缓存maven仓库 RUN mvn install -Dmaven.test.skip=true -Dmaven.repo.local=./.m2 # debug RUN ls -l # 构建打包 RUN mvn -Dmaven.repo.local=./.m2 install -Dmaven.test.skip=true -P prod
标签:maven,RUN,repository,Jar,Maven,Dmaven,Docker,COPY From: https://www.cnblogs.com/zdd-java/p/16801015.html