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

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

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

引用参考:

  1. docker-maven-plugin插件:https://github.com/spotify/docker-maven-plugin
  2. Maven提交服务密码加密:https://maven.apache.org/guides/mini/guide-encryption.html
  3. 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>
.m2文件夹

更多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摘要与日志的摘要一致


Paste_Image.png

5、运行私有仓库的dockerdemo镜像
使用SecureCRT 登录客户端

sudo docker login --username=***pro@gmail.com registry.cn-hangzhou.aliyuncs.com
登录Docker私有仓库

下载仓库镜像

sudo docker pull registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:[镜像版本号]
下载Docker镜像,由于后期才截图,图片的摘要与上文的摘要不同

运行镜像,将Docker容器的8080端口映射本机80端口

docker run -i -t -p 80:8080 registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
启动镜像,Spring Boot启动Tomcat,使用8080端口

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>`





最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,487评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,621评论 2 374
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,611评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,659评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,509评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,380评论 1 274
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,799评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,443评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,739评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,781评论 2 314
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,554评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,400评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,811评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,043评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,330评论 1 253
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,775评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,978评论 2 337

推荐阅读更多精彩内容