Dockerfile的 multi-stage builds模板样例
参考链接:https://docs.docker.com/develop/develop-images/multistage-build/
By default, the stages are not named, and you refer to them by their integer number, starting with 0 for the first FROM instruction. However, you can name your stages, by adding an AS <NAME> to the FROM instruction. This example improves the previous one by naming the stages and using the name in the COPY instruction. This means that even if the instructions in your Dockerfile are re-ordered later, the COPY doesn’t break.
Demo 如下:
FROM maven:latest AS builder #起个别名builder,该阶段主要是构建jar包
COPY . . #拷贝当前目录代码到镜像
RUN mvn -Dmaven.test.skip=true package ###maven构建
FROM openjdk:8-jre-alpine
COPY --from=builder target/xxx-0.0.1.jar /app.jar #--from 指定上个阶段起的别名,会在target目录下将构建好的jar包,拷贝到当前镜像
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ #设置时间同步
&& echo "Asia/Shanghai" > /etc/timezone
EXPOSE 8080 #暴露容器端口
ENTRYPOINT java -jar /app.jar #定义容器启动的命令