plugin插件实现Docker构建并提交到私有仓库

使用Maven插件实现项目打包及Docker构建并提交到私有仓库,以下内容非教程,不适合对Docker及Maven不熟悉者使用

目标:Spring Boot项目打包成可执行的jar文件后,使用docker-maven-plugin(spotify)插件自动构建Docker后上传到阿里云Docker镜像仓库(其他仓库同理)

引用参考:

docker-maven-plugin插件:https://github.com/spotify/docker-maven-plugin

Maven提交服务密码加密:https://maven.apache.org/guides/mini/guide-encryption.html

Maven的settings.xml:

准备工作:

本机安装 Docker 1.9

Eclipse环境(本机使用STS)

Maven环境

阿里云Docker仓库(其他Docker仓库都可以)

如果需要Maven项目部署,需要Maven私服()

SecureCRT (其他终端皆可,仅用于启动Docker测试)

1、 构建Spring Boot项目

使用docker-maven-plugin插件实现Docker构建并提交到私有仓库


项目目录

2、 pom.xml文件中 加入docker-maven-plugin插件

完整pom文件:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 "> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>dockerdemo</artifactId> <version>0.0.1</version> <packaging>jar</packaging> <name>dockerdemo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <Java.version>1.8</java.version> <docker.repostory>registry.cn-hangzhou.aliyuncs.com</docker.repostory> <docker.registry.name>viiso</docker.registry.name> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <resources><!-- 使用@@站位符,输出Dockerfile至docker文件夹 --> <resource> <directory>src/main/docker</directory> <filtering>true</filtering> <includes> <include>**/Dockerfile</include> </includes> <targetPath>../docker</targetPath> </resource> </resources> <plugins> <plugin><!-- 置顶 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.13</version> <executions> <execution> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> <execution> <id>tag-image</id> <phase>package</phase> <goals> <goal>tag</goal> </goals> <configuration> <image>${docker.registry.name}/${project.artifactId}:${project.version}</image> <newName>${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}</newName> </configuration> </execution> <execution> <id>push-image</id> <phase>deploy</phase> <goals> <goal>push</goal> </goals> <configuration> <imageName>${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}</imageName> </configuration> </execution> </executions> <configuration> <!-- 私有仓库配置,需要settings.xml文件配合serverId对应的服务地址 --> <serverId>docker-aliyun</serverId> <registryUrl>registry.cn-hangzhou.aliyuncs.com</registryUrl> <!-- <forceTags>true</forceTags> --> <!--install阶段也上传,否则只有deploy阶段上传--> <pushImage>true</pushImage> <dockerDirectory>target/docker</dockerDirectory> <imageName> ${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version} </imageName> <imageTags> <!--docker的tag为项目版本号、latest--> <imageTag>${git.commit.id.abbrev}</imageTag> <imageTag>latest</imageTag> </imageTags> <resources> <rescource><!-- 将打包文件放入dockerDirectory指定的位置 --> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </rescource> <!-- 输出Dockerfile至docker文件夹,如果不使用占位符,可使用以下配置 --> <!-- <resource> <directory>src/main/docker</directory> <filtering>true</filtering> <includes> <include>**/Dockerfile</include> </includes> <targetPath>../docker</targetPath> </resource> --> </resources> </configuration> </plugin> </plugins> </build> </project>

说明:

Dockerfile构建文件在src/main/docker中

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/fdf9c6e3b2664f12ff5310dff012f42f.html