使用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:http://maven.apache.org/ref/3.3.9/maven-settings/settings.html
准备工作:
- 本机安装 Docker 1.9
- Eclipse环境(本机使用STS)
- Maven环境
- 阿里云Docker仓库(其他Docker仓库都可以)
- 如果需要Maven项目部署,需要Maven私服()
- SecureCRT (其他终端皆可,仅用于启动Docker测试)
1、 构建Spring Boot项目
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<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中
- 如果Dockerfile文件需要maven构建参数(比如需要构建后的打包文件名等),则使用@@占位符(如@project.build.finalName@)原因是Sping Boot 的pom将resource插件的占位符由${}改为@@,非继承Spring Boot 的pom文件,则使用${}占位符
- 如果不需要动态生成Dockerfile文件,则可以将Dockerfile资源拷贝部分放入docker-maven-plugin插件的<resources>配置里
- spring-boot-maven-plugin插件一定要在其他构建插件之上,否则打包文件会有问题。
Dockerfile文件
# Version 0.0.1
FROM java:8
MAINTAINER Ryuu KK "ryuu_kk@163.com"
# 环境变量
ENV WORK_PATH /home/project/dockerdemo
ENV APP_NAME @project.build.finalName@.@project.packaging@
ENV APP_VERSION @project.version@
EXPOSE 8080
#USER
#USER user:group
#VOLUME
VOLUME ["/home/project", "/tmp/data"]
#ADD
#COPY
COPY $APP_NAME $WORK_PATH/
#LABEL
#STOPSIGNAL
#ARG
#ONBUILD
# WORKDIR
WORKDIR $WORK_PATH
# ENTRYPOINT
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom"]
# CMD
CMD ["-jar", "@project.build.finalName@.@project.packaging@"]
3、settings.xml配置私有库的访问
首先使用你的私有仓库访问密码生成主密码:
mvn --encrypt-master-password <password>
其次在settings.xml文件的同级目录创建settings-security.xml文件,将主密码写入:
<?xml version="1.0" encoding="UTF-8"?>
<settingsSecurity>
<master>{Ns0JM49fW9gHMTZ44n*****************=}</master>
</settingsSecurity>
最后使用你的私有仓库访问密码生成服务密码,将生成的密码写入到settings.xml的<services>中:
mvn --encrypt-password <password>
<server>
<id>docker-aliyun</id>
<username>***pro@gmail.com</username>
<password>{D9YIyWYvtYsHayLjIenj***********=}</password>
<configuration>
<email>***pro@gmail.com</email>
</configuration>
</server>
更多settings.xml配置参考:http://maven.apache.org/ref/3.3.9/maven-settings/settings.html
4、执行maven install
如果<pushImage>false</pushImage>则install阶段将不提交Docker镜像,只有maven的deploy阶段才提交。
mvn clean install -Dmaven.test.skip=true -s ~/.m2/settings.xml
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building dockerdemo 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ dockerdemo ---
[INFO] Deleting /home/tony/git/dockerdemo/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ dockerdemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource to ../docker
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ dockerdemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/tony/git/dockerdemo/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ dockerdemo ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ dockerdemo ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ dockerdemo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ dockerdemo ---
[INFO] Building jar: /home/tony/git/dockerdemo/target/dockerdemo-0.0.1.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.4.3.RELEASE:repackage (default) @ dockerdemo ---
[INFO]
[INFO] --- docker-maven-plugin:0.4.13:build (default) @ dockerdemo ---
[INFO] Copying /home/tony/git/dockerdemo/target/dockerdemo-0.0.1.jar -> /home/tony/git/dockerdemo/target/docker/dockerdemo-0.0.1.jar
[INFO] Copying target/docker/dockerdemo-0.0.1.jar -> /home/tony/git/dockerdemo/target/docker/dockerdemo-0.0.1.jar
[INFO] Copying target/docker/Dockerfile -> /home/tony/git/dockerdemo/target/docker/Dockerfile
[INFO] Building image registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
Step 1 : FROM java:8
---> a001fc27db5a
Step 2 : MAINTAINER Ryuu KK "ryuu_kk@163.com"
---> Using cache
---> 9142f19f66e4
Step 3 : ENV WORK_PATH /home/project/dockerdemo
---> Using cache
---> f804b2685414
Step 4 : ENV APP_NAME dockerdemo-0.0.1.jar
---> Using cache
---> 449b34e1b059
Step 5 : ENV APP_VERSION 0.0.1
---> Using cache
---> 434e7a21767b
Step 6 : EXPOSE 8080
---> Using cache
---> b4f12840a5fd
Step 7 : VOLUME /home/project /tmp/data
---> Using cache
---> 6dc6a16375f8
Step 8 : ADD $APP_NAME $WORK_PATH/
---> fb9ca6c55101
Removing intermediate container 0e7d77507c01
Step 9 : COPY . $WORK_PATH/
---> 1f3848c151a5
Removing intermediate container 3ffe74084b83
Step 10 : WORKDIR $WORK_PATH
---> Running in 98819e023aec
---> 14d8dda96d7c
Removing intermediate container 98819e023aec
Successfully built 14d8dda96d7c
[INFO] Built registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
[INFO] Tagging registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1 with latest
[INFO] Pushing registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo]
d8c8f704105a: Pushed
cfb84949120c: Pushed
2071795f387c: Layer already exists
046002f6f2ea: Layer already exists
9425eace6ed9: Layer already exists
9c39e87731b7: Layer already exists
f7ed56f3fce3: Layer already exists
53c779688d06: Layer already exists
60a0858edcd5: Layer already exists
b6ca02dfe5e6: Layer already exists
0.0.1: digest: sha256:e743bfd839a6457a4db7fd22edcb3758ffc39e4e7816c8d00bc24ccf0ede13a5 size: 2424
null: null
[INFO]
[INFO] --- docker-maven-plugin:0.4.13:tag (tag-image) @ dockerdemo ---
[INFO] Creating tag registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1 from registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
[INFO] Pushing registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo]
d8c8f704105a: Layer already exists
cfb84949120c: Layer already exists
2071795f387c: Layer already exists
046002f6f2ea: Layer already exists
9425eace6ed9: Layer already exists
9c39e87731b7: Layer already exists
f7ed56f3fce3: Layer already exists
53c779688d06: Layer already exists
60a0858edcd5: Layer already exists
b6ca02dfe5e6: Layer already exists
0.0.1: digest: sha256:e23d5c731170fd2980b688c34b5a844fac66d9547ba48a41417bd1684425958f size: 2424
null: null
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ dockerdemo ---
[INFO] Installing /home/tony/git/dockerdemo/target/dockerdemo-0.0.1.jar to /home/tony/.m2/repository/com/example/dockerdemo/0.0.1/dockerdemo-0.0.1.jar
[INFO] Installing /home/tony/git/dockerdemo/pom.xml to /home/tony/.m2/repository/com/example/dockerdemo/0.0.1/dockerdemo-0.0.1.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 54.660 s
[INFO] Finished at: 2017-01-08T15:48:33+08:00
[INFO] Final Memory: 43M/303M
[INFO] ------------------------------------------------------------------------
查看阿里云Docker镜像仓库,SHA256摘要与日志的摘要一致
5、运行私有仓库的dockerdemo镜像
使用SecureCRT 登录客户端
sudo docker login --username=***pro@gmail.com registry.cn-hangzhou.aliyuncs.com
下载仓库镜像
sudo docker pull registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:[镜像版本号]
运行镜像,将Docker容器的8080端口映射本机80端口
docker run -i -t -p 80:8080 registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
6、测试
使用服务器ip进行测试
curl http://ip/health
{"status":"UP","diskSpace":{"status":"UP","total":42139451392,"free":38050045952,"threshold":10485760}}
dockerdemo项目代码:https://code.aliyun.com/ryuu.pro/dockerdemo.git
————- 2017/03/21 更新——————
在多Module项目中使用该插件发现的几个问题
<dockerDirectory>target/docker</dockerDirectory>
相对路径在多模块中出现“basedir target does not exist ”问题,使用<dockerDirectory>${project.build.directory}</dockerDirectory>
指定编译路径- 修改Dockerfile文件的生成路径默认到target路径,由于Dockerfile里使用
COPY
无法改变文件路径,如COPY ../xxxx.jar /xxx
时将出现”Permission denied”的问题<resource> <directory>src/main/docker</directory> <filtering>true</filtering> <includes> <include>**/Dockerfile</include> </includes> <targetPath>${project.build.directory}</targetPath> </resource>
- 原文 使用
<imageTag>${git.commit.id.abbrev}</imageTag>
缺少插件<plugin> <groupId>pl.project13.maven</groupId> <artifactId>git-commit-id-plugin</artifactId> <configuration> <failOnNoGitDirectory>false</failOnNoGitDirectory> </configuration> </plugin>
如果不需要git属性插件,可以直接使用
<imageTag>${project.version}</imageTag>
作者:疯狂的橙
链接:http://www.jianshu.com/p/c435ea4c0cc0
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。