dockerfile多階段構建,默認的狀況下,構建的階段是不會命名的,同一個docerfile裏的第一個FROM會以0命名,然而你能夠命名構建階段使用AS起別名,在FROM後面指定,這樣就可使用--copy=別名 ,將該階段的製品拷貝到當前階段的鏡像。java
參考連接:https://docs.docker.com/develop/develop-images/multistage-build/docker
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.app
Demo 以下:
FROM maven:latest AS builder #起個別名builder,該階段主要是構建jar包
COPY . . #拷貝當前目錄代碼到鏡像
RUN mvn -Dmaven.test.skip=true package ###maven構建 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 #定義容器啓動的命令ide