springboot+fabric8多层layer部署docker
springboot多层layer可以更高效的发布docker镜像,fabric8插件可以在pom.xml完成docker的build和push,两者结合大大方便了生产发布镜像文件!
Dockfile代码:
FROM openjdk:18.0.2.1-jdk-oracle as build ARG EXTRACTED=/springbuild RUN mkdir ${EXTRACTED} COPY target/*.jar ${EXTRACTED} RUN cd ${EXTRACTED} && java -Djarmode=layertools -jar *.jar extract FROM openjdk:18.0.2.1-jdk-oracle as deploy WORKDIR /etc/springboot ARG EXTRACTED=/springbuild COPY --from=build ${EXTRACTED}/dependencies/ ./ COPY --from=build ${EXTRACTED}/spring-boot-loader/ ./ COPY --from=build ${EXTRACTED}/snapshot-dependencies/ ./ COPY --from=build ${EXTRACTED}/application/ ./ ENTRYPOINT ["java","org.springframework.boot.loader.JarLauncher"]
Dockerfile文件放在项目根目录下面即可
pom.xml fabric8代码:
<project> <>省略相关业务代码</> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.40.1</version> <configuration> <images> <image> <name>registry.cn-hangzhou.aliyuncs.com/zhqn/${project.artifactId}:${project.version}</name> <build> <dockerFileDir>${project.basedir}</dockerFileDir> <filter>@</filter> </build> </image> </images> <dockerHost>tcp://192.168.10.10:2375</dockerHost> <imagePullPolicy>IfNotPresent</imagePullPolicy> <authConfig> <username>${env.DOCKER.REGISTRY.USERNAME}</username> <password>${env.DOCKER.REGISTRY.PASSWORD}</password> </authConfig> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>build</goal> <goal>push</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
运行命令: mvn clean compile package -Dmaven.test.skip=true -Ddocker.registry.username=test -Ddocker.registry.password=test ,就可以编译和发布docker镜像。
相关文档:
Spring Boot Layer Index