Skip to main content

Google开源其Java容器化工具Jib,简化镜像构建全流程

容器的出现让 Java开发人员比以往任何时候都更接近“编写一次,到处运行”的工作流程,但要对 Java应用程序进行容器化并非易事:你必须编写 Dockerfile,以 root身份运行 Docker守护进程,等待构建完成,最后将镜像推送到远程注册中心。但并非所有的 Java开发人员都是容器专家,像以前那样只需要构建一个 JAR包的日子已经结束了吗?
为了应对这一挑战,谷歌开源了一个 Java容器化工具 Jib,有了这个工具,Java开发人员可以使用他们熟悉的 Java工具来构建容器。Jib是一个快速而简单的容器镜像构建工具,它负责处理将应用程序打包到容器镜像中所需的所有步骤。它不需要你编写 Dockerfile或安装 Docker,而且可以直接集成到 Maven和 Gradle中——只需要将插件添加到构建中,就可以立即将 Java应用程序容器化。
Docker构建流程:
Jib构建流程:
Jib如何让开发变得更美好
Jib利用了 Docker镜像的分层机制,将其与构建系统集成,并通过以下方式优化 Java容器镜像的构建:
  1. 简单——Jib使用 Java开发,并作为 Maven或 Gradle的一部分运行。你不需要编写 Dockerfile或运行 Docker守护进程,甚至无需创建包含所有依赖的大 JAR包。因为 Jib与 Java构建过程紧密集成,所以它可以访问到打包应用程序所需的所有信息。在后续的容器构建期间,它将自动选择 Java构建过的任何变体。
  2. 快速——Jib利用镜像分层和注册表缓存来实现快速、增量的构建。它读取你的构建配置,将你的应用程序组织到不同的层(依赖项、资源、类)中,并只重新构建和推送发生变更的层。在项目进行快速迭代时,Jib只讲发生变更的层(而不是整个应用程序)推送到注册表来节省宝贵的构建时间。
  3. 可重现——Jib支持根据 Maven和 Gradle的构建元数据进行声明式的容器镜像构建,因此,只要输入保持不变,就可以通过配置重复创建相同的镜像。
如何使用 Jib来容器化你的应用程序
Jib可作为 Maven和 Gradle的插件使用,并且只需要做出最少的配置。只需将插件添加到构建定义中并配置目标镜像即可。如果要将镜像推送到私有注册中心,要为 Jib配置所需的秘钥。最简单的方法是使用 docker-credential-gcr之类的凭证助手。Jib还提供了其他的一些规则,用于将镜像构建到 Docker守护进程。
在 Maven 中使用 Jib:


  com.google.cloud.tools

  jib-maven-plugin

  0.9.0

  

    

      gcr.io/my-project/image-built-with-jib

    

  

# Builds to a container image registry.

$ mvn compile jib:build

# Builds to a Docker daemon.

$ mvn compile jib:dockerBuild
在 Gradle 中使用 Jib:
plugins {

  id 'com.google.cloud.tools.jib' version '0.9.0'

}

jib.to.image = 'gcr.io/my-project/image-built-with-jib'
# Builds to a container image registry.

$ gradle jib

# Builds to a Docker daemon.

$ gradle jibDockerBuild
Jib项目地址:
github.com/GoogleContainerTools/jib

Comments

Popular posts from this blog

CKA Simulator Kubernetes 1.22

  https://killer.sh Pre Setup Once you've gained access to your terminal it might be wise to spend ~1 minute to setup your environment. You could set these: alias k = kubectl                         # will already be pre-configured export do = "--dry-run=client -o yaml"     # k get pod x $do export now = "--force --grace-period 0"   # k delete pod x $now Vim To make vim use 2 spaces for a tab edit ~/.vimrc to contain: set tabstop=2 set expandtab set shiftwidth=2 More setup suggestions are in the tips section .     Question 1 | Contexts Task weight: 1%   You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts . Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh , the command should use kubectl . Finally write a second command doing the same thing into ...

OWASP Top 10 Threats and Mitigations Exam - Single Select

Last updated 4 Aug 11 Course Title: OWASP Top 10 Threats and Mitigation Exam Questions - Single Select 1) Which of the following consequences is most likely to occur due to an injection attack? Spoofing Cross-site request forgery Denial of service   Correct Insecure direct object references 2) Your application is created using a language that does not support a clear distinction between code and data. Which vulnerability is most likely to occur in your application? Injection   Correct Insecure direct object references Failure to restrict URL access Insufficient transport layer protection 3) Which of the following scenarios is most likely to cause an injection attack? Unvalidated input is embedded in an instruction stream.   Correct Unvalidated input can be distinguished from valid instructions. A Web application does not validate a client’s access to a resource. A Web action performs an operation on behalf of the user without checkin...