使用dockerfile-maven-plugin将java项目打包并推送到阿里云私服

邹丰羽
2023-12-01
  • linux
  • docker
  • Dockerfile文件
  • dockerfile-maven-plugin POM插件

开始

1.properties配置

        <!-- 无关紧要的配置略过... -->
        <project.version>1.0-SNAPSHOT</project.version>
        <docker.repository.registry>registry.cn-qingdao.aliyuncs.com</docker.repository.registry>
        <docker.repository.namespace>yufei488</docker.repository.namespace>

2.Pom中加入dockerfile-maven-plugin配置

 <!-- dockerfile maven -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.10</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <goals>
                            <goal>build</goal>
                            <goal>push</goal>
                        </goals>
                        <configuration>
                        </configuration>
                    </execution>
                </executions>
                <!--docker镜像相关的配置信息-->
                <configuration>
                    <contextDirectory>${project.basedir}</contextDirectory>
                    <!--下面使用maven settings.xml文件中的 servers,所以要在servers中添加配置,下面将说明-->
                    <!--注:也可以使用username和password标签,useMavenSettingsForAuth不设置,或者为false即可-->
                    <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
                    <repository>${docker.repository.registry}/${docker.repository.namespace}/${project.artifactId}</repository>
                    <goal>push</goal>
                    <tag>${project.version}</tag>
                    <buildArgs>
                        <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>

3.settings中添加server配置

<server>
    <!--该id对应于docker.repository.registry的值-->
    <id>registry.cn-qingdao.aliyuncs.com</id>
    <!--这里是阿里云访问凭证账号-->
    <username>zhanghao</username>
    <!--这里是在阿里云访问凭证密码-->
    <password>mima</password>
    <configuration>
       <!--这是在阿里云注册时填写的邮箱-->
       <email>abcd@qq.com</email>
    </configuration>
</server>

4.Dockerfile编写

FROM java:8

MAINTAINER Yufei<435977043@qq.com>

RUN echo "----------Gateway网关模块开始构建...----------"

ARG JAR_FILE
ENV JAR_NAME ${JAR_FILE}

RUN echo $JAR_FILE

COPY target/${JAR_FILE} /${JAR_FILE}

EXPOSE 8000

WORKDIR /

ENTRYPOINT ["/bin/sh","-c","java -jar $JAR_NAME"]

RUN echo "----------Gateway网关模块构建完毕!!!----------"

5.打包到docker并推送到阿里云私服

#注意 只有deploy才会推送到镜像容器私服,biuld、package等只会编译镜像到本地docker中
#此处是deploy,并且跳过推送到maven私服(也就是说这里只是用了项目打包和推送docker镜像到镜像容器私服)
[root@master ~]# mvn deploy -Dmaven.deploy.skip=true

6.查看结果

# 省略大部分maven打包
[INFO] Compiling 1 source file to /root/back-end/yufei-service/yufei-gateway-server/yufei-gateway-server-starter/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ yufei-gateway-server-starter ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /root/back-end/yufei-service/yufei-gateway-server/yufei-gateway-server-starter/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ yufei-gateway-server-starter ---
[INFO] Changes detected - recompiling the module!
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ yufei-gateway-server-starter ---
[INFO] 
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ yufei-gateway-server-starter ---
[INFO] Building jar: /root/back-end/yufei-service/yufei-gateway-server/yufei-gateway-server-starter/target/yufei-gateway-server-starter-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.3.7.RELEASE:repackage (repackage) @ yufei-gateway-server-starter ---
[INFO] Replacing main artifact with repackaged archive
[INFO] 
[INFO] --- dockerfile-maven-plugin:1.4.10:build (docker-aliyun) @ yufei-gateway-server-starter ---
[INFO] dockerfile: null
[INFO] contextDirectory: /root/back-end/yufei-service/yufei-gateway-server/yufei-gateway-server-starter
[INFO] Building Docker context /root/back-end/yufei-service/yufei-gateway-server/yufei-gateway-server-starter
[INFO] Path(dockerfile): null
[INFO] Path(contextDirectory): /root/back-end/yufei-service/yufei-gateway-server/yufei-gateway-server-starter
[INFO] 
[INFO] Image will be built as registry.cn-qingdao.aliyuncs.com/yufei488/yufei-gateway-server-starter:1.0-SNAPSHOT
[INFO] 
[INFO] Step 1/11 : FROM java:8
[INFO] 
[INFO] Pulling from library/java
[INFO] Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
[INFO] Status: Image is up to date for java:8
[INFO]  ---> d23bdf5b1b1b
[INFO] Step 2/11 : MAINTAINER Yufei<435977043@qq.com>
[INFO] 
[INFO]  ---> Running in 580293a568fa
[INFO] Removing intermediate container 580293a568fa
[INFO]  ---> f92171a04d0b
[INFO] Step 3/11 : RUN echo "----------Gateway网关模块开始构建...----------"
[INFO] 
[INFO]  ---> Running in d2c37f70d8b0
[INFO] ----------Gateway网关模块开始构建...----------
[INFO] Removing intermediate container d2c37f70d8b0
[INFO]  ---> 129bdca7adf4
[INFO] Step 4/11 : ARG JAR_FILE
[INFO] 
[INFO]  ---> Running in 6974cf586d3c
[INFO] Removing intermediate container 6974cf586d3c
[INFO]  ---> df3d8f67deaa
[INFO] Step 5/11 : ENV JAR_NAME ${JAR_FILE}
[INFO] 
[INFO]  ---> Running in adad4b8a7117
[INFO] Removing intermediate container adad4b8a7117
[INFO]  ---> 580a8fb728bf
[INFO] Step 6/11 : RUN echo $JAR_FILE
[INFO] 
[INFO]  ---> Running in d90898a0b126
[INFO] yufei-gateway-server-starter-1.0-SNAPSHOT.jar
[INFO] Removing intermediate container d90898a0b126
[INFO]  ---> 4d62ae709e7c
[INFO] Step 7/11 : COPY target/${JAR_FILE} /${JAR_FILE}
[INFO] 
[INFO]  ---> be82ab2ed69e
[INFO] Step 8/11 : EXPOSE 8000
[INFO] 
[INFO]  ---> Running in aeb52e4bce34
[INFO] Removing intermediate container aeb52e4bce34
[INFO]  ---> 6ce8f91c5724
[INFO] Step 9/11 : WORKDIR /
[INFO] 
[INFO]  ---> Running in 20e7cb50a0d7
[INFO] Removing intermediate container 20e7cb50a0d7
[INFO]  ---> ed31add6dd9f
[INFO] Step 10/11 : ENTRYPOINT ["/bin/sh","-c","java -jar $JAR_NAME"]
[INFO] 
[INFO]  ---> Running in 7b824b034fd7
[INFO] Removing intermediate container 7b824b034fd7
[INFO]  ---> 690e1398ccb8
[INFO] Step 11/11 : RUN echo "----------Gateway网关模块构建完毕!!!----------"
[INFO] 
[INFO]  ---> Running in e7f88c47fc4a
[INFO] ----------Gateway网关模块构建完毕!!!----------
[INFO] Removing intermediate container e7f88c47fc4a
[INFO]  ---> ed81a7aa467b
[INFO] Successfully built ed81a7aa467b
[INFO] Successfully tagged registry.cn-qingdao.aliyuncs.com/yufei488/yufei-gateway-server-starter:1.0-SNAPSHOT
[INFO] 
[INFO] Detected build of image with id ed81a7aa467b
[INFO] Building jar: /root/back-end/yufei-service/yufei-gateway-server/yufei-gateway-server-starter/target/yufei-gateway-server-starter-1.0-SNAPSHOT-docker-info.jar
[INFO] Successfully built registry.cn-qingdao.aliyuncs.com/yufei488/yufei-gateway-server-starter:1.0-SNAPSHOT
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ yufei-gateway-server-starter ---
[INFO] Installing /root/back-end/yufei-service/yufei-gateway-server/yufei-gateway-server-starter/target/yufei-gateway-server-starter-1.0-SNAPSHOT.jar to /root/.m2/repository/top/yufeiii/yufei-gateway-server-starter/1.0-SNAPSHOT/yufei-gateway-server-starter-1.0-SNAPSHOT.jar
[INFO] Installing /root/back-end/yufei-service/yufei-gateway-server/yufei-gateway-server-starter/pom.xml to /root/.m2/repository/top/yufeiii/yufei-gateway-server-starter/1.0-SNAPSHOT/yufei-gateway-server-starter-1.0-SNAPSHOT.pom
[INFO] Installing /root/back-end/yufei-service/yufei-gateway-server/yufei-gateway-server-starter/target/yufei-gateway-server-starter-1.0-SNAPSHOT-docker-info.jar to /root/.m2/repository/top/yufeiii/yufei-gateway-server-starter/1.0-SNAPSHOT/yufei-gateway-server-starter-1.0-SNAPSHOT-docker-info.jar
[INFO] 
[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ yufei-gateway-server-starter ---
[INFO] Skipping artifact deployment
[INFO] 
[INFO] --- dockerfile-maven-plugin:1.4.10:push (docker-aliyun) @ yufei-gateway-server-starter ---
[INFO] The push refers to repository [registry.cn-qingdao.aliyuncs.com/yufei488/yufei-gateway-server-starter]
[INFO] Image c2850145e02a: Preparing
[INFO] Image 35c20f26d188: Preparing
[INFO] Image c3fe59dd9556: Preparing
[INFO] Image 6ed1a81ba5b6: Preparing
[INFO] Image a3483ce177ce: Preparing
[INFO] Image ce6c8756685b: Preparing
[INFO] Image 30339f20ced0: Preparing
[INFO] Image 0eb22bfb707d: Preparing
[INFO] Image a2ae92ffcd29: Preparing
[INFO] Image ce6c8756685b: Waiting
[INFO] Image 30339f20ced0: Waiting
[INFO] Image 0eb22bfb707d: Waiting
[INFO] Image a2ae92ffcd29: Waiting
[INFO] Image c3fe59dd9556: Layer already exists
[INFO] Image 6ed1a81ba5b6: Layer already exists
[INFO] Image 35c20f26d188: Layer already exists
[INFO] Image a3483ce177ce: Layer already exists
[INFO] Image ce6c8756685b: Layer already exists
[INFO] Image 0eb22bfb707d: Layer already exists
[INFO] Image 30339f20ced0: Layer already exists
[INFO] Image a2ae92ffcd29: Layer already exists
[INFO] Image c2850145e02a: Pushing
[INFO] Image c2850145e02a: Pushed
# 到这里就代表推送到私服成功了
[INFO] 1.0-SNAPSHOT: digest: sha256:c2759273f91635bb9144ae78a858d683ca27a6967a8f66cb0f12c9fb68389433 size: 2212
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for back-end 1.0-SNAPSHOT:
[INFO]
[INFO] back-end ........................................... SUCCESS [  1.446 s]
[INFO] yufei-lib .......................................... SUCCESS [  0.041 s]
[INFO] yufei-lib-common ................................... SUCCESS [  8.164 s]
[INFO] yufei-lib-web ...................................... SUCCESS [  4.910 s]
[INFO] yufei-lib-oauth2 ................................... SUCCESS [  0.241 s]
[INFO] yufei-lib-mybatis .................................. SUCCESS [  0.365 s]
[INFO] yufei-lib-eureka ................................... SUCCESS [  0.143 s]
[INFO] yufei-lib-nacos .................................... SUCCESS [  1.822 s]
[INFO] yufei-authentication-server ........................ SUCCESS [  0.016 s]
[INFO] yufei-authentication-server-core ................... SUCCESS [  4.163 s]
[INFO] yufei-authentication-server-starter ................ SUCCESS [  4.619 s]
[INFO] yufei-service ...................................... SUCCESS [  0.022 s]
[INFO] yufei-eureka-server ................................ SUCCESS [  0.062 s]
[INFO] yufei-eureka-server-core ........................... SUCCESS [  2.170 s]
[INFO] yufei-eureka-server-starter ........................ SUCCESS [  1.878 s]
[INFO] yufei-gateway-server ............................... SUCCESS [  0.042 s]
[INFO] yufei-gateway-server-core .......................... SUCCESS [  0.591 s]
[INFO] yufei-gateway-server-starter ....................... SUCCESS [05:06 min]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  05:38 min
[INFO] Finished at: 2021-02-25T00:27:54+08:00
[INFO] ------------------------------------------------------------------------

7.测试

[root@master back-end]# docker pull registry.cn-qingdao.aliyuncs.com/yufei488/yufei-gateway-server-starter:1.0-SNAPSHOT
1.0-SNAPSHOT: Pulling from yufei488/yufei-gateway-server-starter
Digest: sha256:c2759273f91635bb9144ae78a858d683ca27a6967a8f66cb0f12c9fb68389433
Status: Image is up to date for registry.cn-qingdao.aliyuncs.com/yufei488/yufei-gateway-server-starter:1.0-SNAPSHOT
registry.cn-qingdao.aliyuncs.com/yufei488/yufei-gateway-server-starter:1.0-SNAPSHOT

可能写的不全,有问题请留言。
如果有帮助到兄弟们,也请留言让我知道,抱拳!

 类似资料: