6

使用Jib来容器化Spring Boot应用

 3 years ago
source link: http://dockone.io/article/2434106
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

【把做大会】本文讲解了如何在不安装任何Docker客户端或使用Dockerfiles的情况下,通过使用Google的Jib为Spring Boot应用程序创建Docker或兼容OCI的镜像。

在本文中,我们将学习如何在不安装Docker客户端或使用Dockerfile的情况下为我们的Spring Boot应用程序创建Docker或兼容OCI的镜像。我们将在Jib的帮助下完成这一切。

什么是Jib?

Jib是谷歌开发的一个Java容器化工具,它可以让Java开发者使用Maven、Gradle等构建工具构建容器。

但这并不是Jib真正有趣的地方,因为你不需要知道任何关于安装Docker、维护Dockerfiles等的知识。作为一个开发者,你只关心你将产生的程序包(jar、war等),你不需要理睬任何与Docker相关的废话(比如构建/推送等)。

Jib vs. Docker构建流程 (Image source Google Cloud)

哇,这真的很厉害!但怎么个厉害法?

如何玩转Jib

有了Jib,你可以通过在你的pom.xmlbuild.gradle文件中添加一个Maven或Gradle插件,在短时间内将你的Java应用容器化,就是这么简单。我们将首先介绍Maven,在后面的文章中,我们会提到Gradle,那我们就开始吧。

我们将使用Spring Initializr来生成一个示例的Spring Boot项目。我们的Spring Boot应用程序的源代码可以在这里中找到。它只是在通过Jib推送镜像并通过Docker运行镜像时打印一条Hello消息。

一旦我们把IDE配置完毕,就可以进行下一步了。

配置Maven

<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>2.6.0</version>
<configuration>
  <from>
     <image>gcr.io/distroless/java:11</image>
  </from>
  <to>
     <image>registry.hub.docker.com/hiashish/spring-boot-jib-image</image>
  </to>
</configuration>
</plugin>

对于Maven来说,你可以把上面的内容粘贴到你的pom.xml的插件部分就可以。但我想在这里解释一下<from><image>标签。

<from>配置用于构建应用程序的基础镜像

通常情况下,你不需要提供<from>作为默认值,因为它使用的是无发行版的Java 8镜像。然而,我使用的是Java 11,所以我需要在这里明确配置这一项。此外,根据你的实际场景,你可能想要使用不同的基础镜像。

<image>指的是将推送到容器注册中心的目标映像名称

我使用的是Docker注册表,但你可以使用任何云提供商的(ECS、GCR、ACR)容器注册。

关于插件使用的其他配置选项,你可以参考文档

配置注册中心的访问密钥

如果要推送镜像,我们需要在Maven的settings.xml文件中添加注册中心的证书。由于我们只是在做演示,所以这样提供凭证是可以的,但要避免在真实世界的项目中使用,因为它不安全。你可能希望按照这里提到的方式来确保证书的安全。
<server>
<id>registry.hub.docker.com</id>
<username>username</username>
<password>password</password>
</server>

要建立一个镜像,我们可以通过以下方式来实现。

在IDE中构建

例如,在IntelliJ中,你可以进入项目的Maven视图,然后进入Plugins>jib下,然后右键点击并运行Maven构建。你可能想创建一个IntelliJ运行配置,可以运行Maven目标,如clean、编译等,然后推送你的镜像。

使用命令行

只需运行下面的命令来构建应用程序的镜像。请确保你已经安装了Maven。
mvn compile jib:build

这将触发编译、构建流程,然后将应用程序的镜像推送到配置的容器注册中心。

以下是输出结果。
ashish@MacBook-Air springboot % mvn compile jib:build
[INFO] Scanning for projects…
[INFO]
[INFO] — — — — — — — — — — < com.example:spring-boot-jib > — — — — — — — — — — -
[INFO] Building springboot 0.0.1-SNAPSHOT
[INFO] — — — — — — — — — — — — — — — — [ jar ] — — — — — — — — — — — — — — — — -
[INFO]
[INFO] — — maven-resources-plugin:3.1.0:resources (default-resources) @ spring-boot-jib — -
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] — — maven-compiler-plugin:3.8.1:compile (default-compile) @ spring-boot-jib — -
[INFO] Nothing to compile — all classes are up to date
[INFO]
[INFO] — — jib-maven-plugin:2.6.0:build (default-cli) @ spring-boot-jib — -
[WARNING] ‘mainClass’ configured in ‘maven-jar-plugin’ is not a valid Java class: ${start-class}
[INFO]
[INFO] Containerizing application to registry.hub.docker.com/hiashish/spring-boot-jib-image…
[WARNING] Base image ‘gcr.io/distroless/java:11’ does not use a specific image digest — build may not be reproducible
[INFO] Using credentials from Maven settings file for registry.hub.docker.com/hiashish/spring-boot-jib-image
[INFO] Using base image with digest: sha256:b25c7a4f771209c2899b6c8a24fda89612b5e55200ab14aa10428f60fd5ef1d1
[INFO]
[INFO] Executing tasks:
[INFO]
[INFO] Executing tasks:
[INFO]
[INFO] Executing tasks:
[INFO] [======= ] 25.0% complete
[INFO]
[INFO] Executing tasks:
[INFO] [======= ] 25.0% complete
[INFO] > pushing blob sha256:6508f436f385b3751366f90b6…
[INFO]
[INFO] Executing tasks:
[INFO] [======= ] 25.0% complete
[INFO] > pushing blob sha256:6508f436f385b3751366f90b6…
[INFO] > pushing blob sha256:c5e22041fc97b838b93a2e18d…
[INFO]
[INFO] Executing tasks:
[INFO] [======= ] 25.0% complete
[INFO] > pushing blob sha256:6508f436f385b3751366f90b6…
[INFO] > pushing blob sha256:c5e22041fc97b838b93a2e18d…
[INFO] > pushing blob sha256:b25902383f9ee26808b68ca62…
[INFO]
[INFO] Executing tasks:
[INFO] [======= ] 25.0% complete
[INFO] > pushing blob sha256:6508f436f385b3751366f90b6…
[INFO] > pushing blob sha256:c5e22041fc97b838b93a2e18d…
[INFO] > pushing blob sha256:b25902383f9ee26808b68ca62…
[INFO] > checking base image layer sha256:31eb28996804…
[INFO]
[INFO] Executing tasks:
[INFO] [======== ] 27.8% complete
[INFO] > pushing blob sha256:c5e22041fc97b838b93a2e18d…
[INFO] > pushing blob sha256:b25902383f9ee26808b68ca62…
[INFO] > checking base image layer sha256:31eb28996804…
[INFO]
[INFO] Executing tasks:
[INFO] [========= ] 30.6% complete
[INFO] > pushing blob sha256:c5e22041fc97b838b93a2e18d…
[INFO] > checking base image layer sha256:31eb28996804…
[INFO]
[INFO] Executing tasks:
[INFO] [========== ] 33.3% complete
[INFO] > checking base image layer sha256:31eb28996804…
[INFO]
[INFO] Executing tasks:
[INFO] [=========== ] 35.0% complete
[INFO]
[INFO] Executing tasks:
[INFO]
[INFO]
[INFO]
[INFO] Container entrypoint set to [java, -cp, /app/resources:/app/classes:/app/libs/*, com.jib.example.spring.SpringbootApplication]
[INFO]
[INFO] Built and pushed image as registry.hub.docker.com/hiashish/spring-boot-jib-image
[INFO] Executing tasks:
[INFO] [=========================== ] 91.7% complete
[INFO] > launching layer pushers
[INFO]
[INFO] — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
[INFO] BUILD SUCCESS
[INFO] — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
[INFO] Total time: 8.746 s
[INFO] Finished at: 2020–11–16T02:34:33+05:30
[INFO] — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

我们已经成功地将镜像(镜像名称:spring-boot-jib-image)推送到Docker注册中心。现在我们可以使用Docker来运行这个镜像,

运行镜像

正如你所看到的,我们的应用程序正在一个容器内运行。现在只需运行curl命令,你就会收到来自Spring Boot应用程序的hello消息。

Hello消息
在这篇文章中,我们已经了解了如何在没有Docker的情况下对我们的Java应用进行容器化。另外,通过Jib,你也可以使用Docker来构建镜像,当然这并不是Jib的最佳实践,不值一提。在你的Java应用中使用Jib的其他好处包括:与Java应用更紧密的集成,更快的构建速度,可重复的构建,得到Google的支持等等。如果你想要详细了解Jib的更多特性与优势,可以访问这个链接

原文链接:Containerizing SpringBoot Application With Jib(翻译:小灰灰)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK