Creating a Docker image of a Java application usually comes with the hustle of having a correct Dockerfile, a running docker daemon for a local build or delegate it to a CI tool like Jenkins, Travis CI, etc. That is, it was! With Jib, it's a straightforward process only bound to Maven or Gradle.

What's Jib ?

The Google blog post introducing Jib offers a really good definition:

Google的开源Java容器化程序,可让Java开发人员使用他们所知道的Java工具来构建容器。 Jib是一个快速,简单的容器映像生成器,可以处理将应用程序打包为容器映像的所有步骤。 它不需要您编写Dockerfile或安装docker,它直接集成到Maven和Gradle中-只需将插件添加到构建中,即可立即将Java应用程序容器化。

基本上就是这样。 Jib可以帮助我们构建docker容器,而无需遵循通常的过程,而仅使用Maven或Gradle插件即可。 以下是一些图像,可帮助您可视化通常的docker构建流程与臂架构建流程之间的差异。

Docker构建流程:

解docker包 docker jitsi_docker

臂架构建流程:

解docker包 docker jitsi_Java_02

We can go from our code to an image pushed in our registry in one shot, without even having a running local docker daemon, that's awesome right ?! And yes, you don't actually need a running local docker daemon to push your image to a registry. Jib natively supports pushing to common registries such as Docker Hub, Google Container Registry (GCR), Amazon Elastic Container Registry (ECR) and Azure Container Registry (ACR) and for a custom one, just prefix the url to your image name.

Usage

Take a trip to the Jib Maven Plugin Quickstart or if you are using Gradle, go here instead. Read it in order to setup things like authentication to your registry. I will take this sample Spring Boot project available on Github as an example. After added this to the pom:



<plugins>
    ...
    <plugin>
        <groupId>com.google.cloud.tools</groupId>
        <artifactId>jib-maven-plugin</artifactId>
        <version>${jib.version}</version>
        <configuration>
            <from>
                <image>openjdk:8u222-jre</image>
            </from>
            <to>
                <image>docker.io/mlniang/jib-example:${project.version}</image>
            </to>
            <container>
                <entrypoint>
                    <shell>sh</shell>
                    <option>-c</option>
                    <arg>chmod +x /entrypoint.sh && sync && /entrypoint.sh</arg>
                </entrypoint>
                <ports>
                    <port>8080</port>
                </ports>
                <environment>
                    <SPRING_OUTPUT_ANSI_ENABLED>ALWAYS</SPRING_OUTPUT_ANSI_ENABLED>
                </environment>
                <creationTime>USE_CURRENT_TIMESTAMP</creationTime>
            </container>
        </configuration>
    </plugin>
    ...
</plugins>



The command mvn compile jib:build built and pushed my image to docker hub. Assuming by now, you have read the documentation, here is a quick explanation of this configuration:

  • 创建并推送图像mlniang/jib-example:<pom_project_version>使用openjdk:8u222-jre作为基本图片。在图像中的任何容器中,作为入口,执行位于src / main / jib / entrypoint.sh其中包含用于以独立模式运行Spring Boot应用程序的Java命令。 Jib会自动将图像中的任何文件添加到图像src / main / jib该目录将作为我们容器的根目录。露出港口8080将来可以访问我们的Spring Boot应用程序的任何容器。设置环境变量SPRING_OUTPUT_ANSI_ENABLED任何容器。将容器的创建时间设置为当前时间戳(默认情况下它将使用UNIX EPOCH)。

请注意,您的代码需要在编译之前臂架:建造目标和第一次运行可能需要一些时间,但是由于层缓存,后续构建会更快。 如果您只想构建图片而不推送图片,则可以使用臂架:dockerBuild目标代替。 但是,对于这个,您必须具有一个正在运行的docker守护程序。

您可以绑定执行臂架:建造要么臂架:dockerBuild to an existing Maven lifecycle. If we want the 臂架:建造 goal to execute when we package our app, we will add to the plugin configuration:



...
<plugin>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>jib-maven-plugin</artifactId>
    <version>${jib.version}</version>
    <configuration>
            ...
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
</plugin>
...



现在,mvn清洁包装足以完成工作。

最后一件事:您可以使用命令行参数来覆盖您的配置。 一种用例是,当我们希望图像的当前标记版本为最新版本时。 在执行通常的package命令后,我可以进行以下操作:



$ mvn clean package -Djib.to.image=docker.io/mlniang/jib-example:latest



甚至没有修改我的pom。

与Jib有关的工作还很多,例如构建tar存档,高级配置等。请阅读文档,并希望该工具对您的日常生活有所帮助。