idea-springboot项目打包镜像推送至harbor仓库

miaodi
发布于 2020-10-21 / 1399 阅读
0

idea-springboot项目打包镜像推送至harbor仓库

1. 概述

1.1 开发环境

  • 系统: macos Catalina 10.15.6
  • 开发工具: IntelliJ IDEA 2020.2.1
  • Springboot版本: SpringBoot 2.2.6.RELEASE
  • Maven版本: Apache Maven 3.6.3
  • 开发机不用安装docker

1.2 Harbor仓库

  • Harbor 版本 v1.9.4-49eb397c

1.3 其他


2020.11.3更新

新建了一个springdemo。方便直观的看到文件结构: https://gitlab.bfmiaodi.cn/miaodi/helloHarbor


2. 相关插件以及配置

2.0 一个SpringBoot的demo项目

2.1 pom.xml中添加插件用于打包:

 <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.2.6.RELEASE</version>
</plugin>

2.2 pom.xml中添加插件用于跳过测试用例:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>

2.3 pom.xml中添加dockerfile插件用于推送至harbor:

  <plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.4.13</version>
    <executions>
        <execution>
            <id>default</id>
            <goals>
                <!--如果package时不想用docker打包,就注释掉这个goal
                <goal>build</goal>
                <goal>push</goal>-->
            </goals>
        </execution>
    </executions>
    <configuration>
        <!-- 上下文路径配置,此处设置为项目根路径 用来读取Dockerfile-->
        <contextDirectory>${project.basedir}</contextDirectory>
        <!--使用Maven的setting.xml来配置账号密码-->
        <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
        <!--上传路径/镜像构建名: Harbor地址/Harbor项目名/springboot项目名-->
        <repository>${docker.registry}/${docker.image.prefix}/${project.artifactId}</repository>
        <!-- 标签,最终会显示在Harbor镜像的标签中 -->
        <tag>${project.version}</tag>
        <!-- 作为Dockerfile 文件传入,也就是Dockerfile 中 ARG JAR_FILE的值-->
        <buildArgs>
            <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
        </buildArgs>
    </configuration>
</plugin>

2.4 pom.xml中添加properties:(一般添加到父项目中)

<properties>
  <docker.image.prefix>private</docker.image.prefix>
  <docker.registry>harbor.bfmiaodi.top</docker.registry>
</properties>

docker.registry改为自己的harbor仓库的域名 docker.image.prefix改为自己的仓库名 image.png


2.5 配置harbor的账号密码

在maven的配置文件中配置,配置文件在这个位置: image.png

找到<servers/>节点,在这个节点中加上harbor的地址、账号、密码:

<server>
  <id>harbor.bfmiaodi.top</id>       
  <username>admin</username>
  <password>yourpassword</password>
</server>

2.6 在项目的根目录创建dockerfile

dockerfile内容:

FROM harbor.bfmiaodi.top/public/java:8
ARG JAR_FILE
ADD target/${JAR_FILE} app.jar
ENTRYPOINT ["java", "-Duser.timezone=GMT+08", "-jar", "/app.jar"]

这里用的是自己仓库里的harbor.bfmiaodi.top/public/java:8,内网环境拉取的速度更快。可以改成自己harbor里面的java,也可以改为FROM java:8 最后一行的"-Duser.timezone=GMT+08"是将时区改为东8区。java:8的时区默认是0时区的,当然也可以用自己制作好的jdk镜像。

dockerfile需要放在项目的根目录,通过修改<contextDirectory>${project.basedir}</contextDirectory>可以将dockerfile放在其他目录。


3. 制作镜像并推送到harbor

3.1 贴下代码:

可以直接看3.2

dong-mac@Dong_Mac_1 miaodi-serverchan % mvn clean
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< cn.bfmiaodi:miaodi-serverchan >--------------------
[INFO] Building miaodi-serverchan latest
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ miaodi-serverchan ---
[INFO] Deleting /Users/dong-mac/workspace/git/miaodi-parent/miaodi-serverchan/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.390 s
[INFO] Finished at: 2020-10-21T17:27:36+08:00
[INFO] ------------------------------------------------------------------------

dong-mac@Dong_Mac_1 miaodi-serverchan % mvn install 
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< cn.bfmiaodi:miaodi-serverchan >--------------------
[INFO] Building miaodi-serverchan latest
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ miaodi-serverchan ---
[INFO] Installing /Users/dong-mac/workspace/git/miaodi-parent/miaodi-serverchan/target/miaodi-serverchan-latest.jar to /Users/dong-mac/.m2/repository/cn/bfmiaodi/miaodi-serverchan/latest/miaodi-serverchan-latest.jar
[INFO] Installing /Users/dong-mac/workspace/git/miaodi-parent/miaodi-serverchan/pom.xml to /Users/dong-mac/.m2/repository/cn/bfmiaodi/miaodi-serverchan/latest/miaodi-serverchan-latest.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.772 s
[INFO] Finished at: 2020-10-21T17:27:47+08:00
[INFO] ------------------------------------------------------------------------

dong-mac@Dong_Mac_1 miaodi-serverchan % mvn dockerfile:build
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< cn.bfmiaodi:miaodi-serverchan >--------------------
[INFO] Building miaodi-serverchan latest
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- dockerfile-maven-plugin:1.4.13:build (default-cli) @ miaodi-serverchan ---
[INFO] dockerfile: null
[INFO] contextDirectory: /Users/dong-mac/workspace/git/miaodi-parent/miaodi-serverchan
[INFO] Building Docker context /Users/dong-mac/workspace/git/miaodi-parent/miaodi-serverchan
[INFO] Path(dockerfile): null
[INFO] Path(contextDirectory): /Users/dong-mac/workspace/git/miaodi-parent/miaodi-serverchan
[INFO] 
[INFO] Image will be built as harbor.bfmiaodi.top/private/miaodi-serverchan:latest
[INFO] 
[INFO] Step 1/4 : FROM harbor.bfmiaodi.top/public/java:8
[INFO] 
[INFO] Pulling from public/java
[INFO] Digest: sha256:f841a2abd0422364ec94bb633a56707a38c330179f2bbccebd95f9aff4a36808
[INFO] Status: Image is up to date for harbor.bfmiaodi.top/public/java:8
[INFO]  ---> d23bdf5b1b1b
[INFO] Step 2/4 : ARG JAR_FILE
[INFO] 
[INFO]  ---> Using cache
[INFO]  ---> 9ff0cf27e69a
[INFO] Step 3/4 : ADD target/${JAR_FILE} app.jar
[INFO] 
[INFO]  ---> 949ed33179a2
[INFO] Step 4/4 : ENTRYPOINT ["java", "-Duser.timezone=GMT+08", "-jar", "/app.jar"]
[INFO] 
[INFO]  ---> Running in f402ad8deacb
[INFO] Removing intermediate container f402ad8deacb
[INFO]  ---> 62f8de91149a
[INFO] Successfully built 62f8de91149a
[INFO] Successfully tagged harbor.bfmiaodi.top/private/miaodi-serverchan:latest
[INFO] 
[INFO] Detected build of image with id 62f8de91149a
[INFO] Building jar: /Users/dong-mac/workspace/git/miaodi-parent/miaodi-serverchan/target/miaodi-serverchan-latest-docker-info.jar
[INFO] Successfully built harbor.bfmiaodi.top/private/miaodi-serverchan:latest
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  9.497 s
[INFO] Finished at: 2020-10-21T17:30:20+08:00
[INFO] ------------------------------------------------------------------------

dong-mac@Dong_Mac_1 miaodi-serverchan % mvn dockerfile:push 
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< cn.bfmiaodi:miaodi-serverchan >--------------------
[INFO] Building miaodi-serverchan latest
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- dockerfile-maven-plugin:1.4.13:push (default-cli) @ miaodi-serverchan ---
[INFO] The push refers to repository [harbor.bfmiaodi.top/private/miaodi-serverchan]
[INFO] Image 68018444df75: 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 a3483ce177ce: Layer already exists
[INFO] Image 35c20f26d188: Layer already exists
[INFO] Image 68018444df75: Pushing
[INFO] Image 6ed1a81ba5b6: Layer already exists
[INFO] Image c3fe59dd9556: Layer already exists
[INFO] Image ce6c8756685b: Layer already exists
[INFO] Image 30339f20ced0: Layer already exists
[INFO] Image a2ae92ffcd29: Layer already exists
[INFO] Image 0eb22bfb707d: Layer already exists
[INFO] Image 68018444df75: Pushed
[INFO] latest: digest: sha256:559f03b2ecf1c19ec31ca97d226936b41e8fb6e09e865a228367fe277bf571ba size: 2212
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.933 s
[INFO] Finished at: 2020-10-21T17:31:15+08:00
[INFO] ------------------------------------------------------------------------

3.2 总共就4个步骤

清除相关文件: mvn clean 生成项目jar文件并加入到本地maven仓库,这一步骤之后可以先java -jar xxxx测试下生成的jar文件是否正确。 mvn install 根据dockerfile创建镜像: mvn dockerfile:build 将镜像推送到harbor仓库 mvn dockerfile:push

4. 在另一台机器上部署镜像

4.1 先到harbor中查看下已上传的镜像

image.png 点击pull命令可以复制: docker pull harbor.bfmiaodi.top/private/miaodi-serverchan:latest

4.2 docker客户端登录harbor并拉取镜像

## 登录harbor仓库

[root@k-21 ~]# docker login harbor.bfmiaodi.top
Username: admin
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

## 拉取镜像

[root@k-21 ~]# docker pull harbor.bfmiaodi.top/private/miaodi-serverchan:lastest
Error response from daemon: manifest for harbor.bfmiaodi.top/private/miaodi-serverchan:lastest not found: manifest unknown: manifest unknown
[root@k-21 ~]# docker pull harbor.bfmiaodi.top/private/miaodi-serverchan:latest
latest: Pulling from private/miaodi-serverchan
7448db3b31eb: Already exists
c36604fa7939: Already exists
29e8ef0e3340: Already exists
a0c934d2565d: Already exists
a360a17c9cab: Already exists
cfcc996af805: Already exists
2cf014724202: Already exists
4bc402a00dfe: Already exists
9240aa815003: Pull complete
Digest: sha256:559f03b2ecf1c19ec31ca97d226936b41e8fb6e09e865a228367fe277bf571ba
Status: Downloaded newer image for harbor.bfmiaodi.top/private/miaodi-serverchan:latest
harbor.bfmiaodi.top/private/miaodi-serverchan:latest

## 查看镜像

[root@k-21 ~]# docker images
REPOSITORY                                      TAG                 IMAGE ID            CREATED             SIZE
harbor.bfmiaodi.top/private/miaodi-serverchan   latest              62f8de91149a        10 hours ago        704MB
harbor.bfmiaodi.top/private/miaodi-serverchan   1.0-SNAPSHOT        f62720f87e65        2 days ago          704MB
harbor.bfmiaodi.top/public/miaodi-serverchan    1.0-SNAPSHOT        f62720f87e65        2 days ago          704MB
bfmiaodi/server_chan                            latest              8488d05e98d9        3 months ago        698MB
dl-bili/test                                    latest              b543d189eed3        3 months ago        705MB
dl-bili/eureka-server                           latest              86d85979359e        3 months ago        698MB
redis                                           latest              235592615444        4 months ago        104MB
nginx                                           latest              2622e6cca7eb        4 months ago        132MB
mysql                                           latest              be0dbf01a0f3        4 months ago        541MB
centos                                          7                   b5b4d78bc90c        5 months ago        203MB
java                                            8                   d23bdf5b1b1b        3 years ago         643MB

## 创建容器 这里--network=host直接使用宿主机的网络,也可以-p9090:9090做端口映射

[root@k-21 ~]# docker run -d --name serverchan --network=host harbor.bfmiaodi.top/private/miaodi-serverchan
2ccf5d93e4066a3b07757baa5ad79cfa98599a1d738f3ed8388136fc25bccfa4

## 查看日志

[root@k-21 ~]# docker logs -f serverchan
2020-10-22 03:28:08.358  INFO 1 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
LOGBACK: No context given for c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@1518864111

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.6.RELEASE)

2020-10-22 03:28:08.707  INFO 1 --- [           main] c.b.serverChan.ServerChanApplication     : The following profiles are active: dev
2020-10-22 03:28:10.523  INFO 1 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=2651a673-760f-3117-b002-03f9635e0344
2020-10-22 03:28:11.609  INFO 1 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-10-22 03:28:12.324  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9090 (http)
2020-10-22 03:28:12.348  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-10-22 03:28:12.349  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-10-22 03:28:12.467  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-10-22 03:28:12.468  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3734 ms

## 浏览器访问下spring项目 未截图

5. 相关报错

5.1 mvn dockerfile:build报错

相关报错: 十月 21, 2020 12:08:14 下午 com.spotify.docker.client.shaded.org.apache.http.impl.execchain.RetryExec execute 信息: Retrying request to {}->unix://localhost:80

原因:默认读取的系统环境变量: DOCKER_HOST,环境变量未添加成功。 解决:linux/macos添加系统变量: export DOCKER_HOST=tcp://harbor.bfmiaodi.top

5.2 mac环境idea的terminal中每次都要重新执行export DOCKER_HOST=tcp://harbor.bfmiaodi.top

解决:

echo  "export DOCKER_HOST=tcp://harbor.bfmiaodi.top" >> ~/.bash_profile
echo  "source ~/.bash_profile" >> ~/.zshrc

这样每次启动terminal都会自动执行export DOCKER_HOST=tcp://harbor.bfmiaodi.top