从源码到镜像:使用 Docker 多阶段构建 Spring Boot 镜像
Spring Boot Docker Maven About 4,742 words多阶段构建
阶段一
使用Maven
打包,拷贝工程源码、pom.xml
文件。
阶段二
从阶段一拷贝构建好的jar
包,配置运行参数等,完成 Spring Boot 应用构建镜像。
Dockerfile
FROM maven:3.8.5-eclipse-temurin-11-alpine AS build
WORKDIR /app
COPY src ./src
COPY pom.xml .
COPY settings.xml .
RUN mvn -s ./settings.xml -f ./pom.xml clean package -Dmaven.test.skip=true
FROM openjdk:11-oraclelinux8
COPY --from=build /app/target/*.jar /app/app.jar
ENTRYPOINT ["java", "-jar", "/app/app.jar"]
构建
docker build -t myapp2 .
输出
[+] Building 124.4s (14/14) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 32B 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/openjdk:11-oraclelinux8 0.0s
=> [internal] load metadata for docker.io/library/maven:3.8.5-eclipse-temurin-11-alpine 0.0s
=> [build 1/6] FROM docker.io/library/maven:3.8.5-eclipse-temurin-11-alpine 0.0s
=> [internal] load build context 0.1s
=> => transferring context: 2.58kB 0.0s
=> CACHED [stage-1 1/2] FROM docker.io/library/openjdk:11-oraclelinux8 0.0s
=> CACHED [build 2/6] WORKDIR /app 0.0s
=> CACHED [build 3/6] COPY src ./src 0.0s
=> CACHED [build 4/6] COPY pom.xml . 0.0s
=> CACHED [build 5/6] COPY settings.xml . 0.0s
=> [build 6/6] RUN mvn -s ./settings.xml -f ./pom.xml clean package -Dmaven.test.skip=true 118.8s
=> [stage-1 2/2] COPY --from=build /app/target/*.jar /app/app.jar 2.1s
=> exporting to image 2.0s
=> => exporting layers 1.9s
=> => writing image sha256:42d3fb1b410fd0152a5f5fa376b0fbe33135049e1aaa56ac61cdc09c4cef2950 0.0s
=> => naming to docker.io/library/myapp2
运行
docker run -p 8080:8080 -d myapp2
Views: 1,563 · Posted: 2022-10-23
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...