阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

221次阅读
没有评论

共计 11837 个字符,预计需要花费 30 分钟才能阅读完成。

使用 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 项目

使用 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 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>
使用 docker-maven-plugin 插件实现 Docker 构建并提交到私有仓库

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

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

Paste_Image.png

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

sudo docker login --username=***pro@gmail.com registry.cn-hangzhou.aliyuncs.com
使用 docker-maven-plugin 插件实现 Docker 构建并提交到私有仓库

登录 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
使用 docker-maven-plugin 插件实现 Docker 构建并提交到私有仓库

启动镜像,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>

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-11/148202.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计11837字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中