Dockerizing Java Apps using Jib – 使用Jib对Java应用程序进行Docker化

最后修改: 2018年 10月 19日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll take a look at Jib and how it simplifies containerization of Java applications.

在本教程中,我们将看看Jib以及它如何简化Java应用程序的容器化。

We’ll take a simple Spring Boot application and build its Docker image using Jib. And then we’ll also publish the image to a remote registry.

我们将采取一个简单的Spring Boot应用程序,并使用Jib构建其Docker镜像。然后我们还将把镜像发布到一个远程注册中心。

And make sure also to refer to our tutorial about dockerizing Spring Boot applications using dockerfile and docker tool.

并确保参考我们关于使用dockerfile和docker工具对Spring Boot应用程序进行停靠的教程。

2. Introduction to Jib

2.Jib简介

Jib is an open-source Java tool maintained by Google for building Docker images of Java applications. It simplifies containerization since with it, we don’t need to write a dockerfile.

Jib是一个开源的Java工具,由Google维护,用于构建Java应用程序的Docker镜像。它简化了容器化,因为有了它,我们不需要写一个dockerfile。

And actually, we don’t even have to have docker installed to create and publish the docker images ourselves.

实际上,我们甚至不需要安装docker来创建和发布docker镜像。

Google publishes Jib as both a Maven and a Gradle plugin. This is nice because it means that Jib will catch any changes we make to our application each time we build. This saves us separate docker build/push commands and simplifies adding this to a CI pipeline.

谷歌将Jib作为Maven和Gradle插件发布。这很好,因为这意味着Jib将在我们每次构建时捕获我们对应用程序的任何更改。这为我们节省了单独的docker build/push命令,并简化了将其添加到CI管道中。

There are a couple of other tools out there, too, like Spotify’s docker-maven-plugin and dockerfile-maven plugins, though the former is now deprecated and the latter requires a dockerfile.

还有一些其他的工具,比如Spotify的docker-maven-plugindockerfile-maven插件,不过前者现在已经废弃了,后者需要一个dockerfile

3. A Simple Greeting App

3.一个简单的问候应用程序

Let’s take a simple spring-boot application and dockerize it using Jib. It’ll expose a simple GET endpoint:

让我们采取一个简单的spring-boot应用程序,并使用Jib将其docker化。它将暴露一个简单的GET端点。

http://localhost:8080/greeting

Which we can do quite simply with a Spring MVC controller:

我们可以用Spring MVC控制器很简单地做到这一点。

@RestController
public class GreetingController {

    private static final String template = "Hello Docker, %s!";
    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", 
        defaultValue="World") String name) {
        
        return new Greeting(counter.incrementAndGet(),
          String.format(template, name));
    }
}

4. Preparing the Deployment

4.准备部署

We’ll also need to set ourselves up locally to authenticate with the Docker repository we want to deploy to.

我们还需要在本地设置自己,以便与我们想要部署的Docker仓库进行认证。

For this example, we’ll provide our DockerHub credentials to .m2/settings.xml:

在这个例子中,我们将.m2/settings.xml提供我们的DockerHub凭证。

<servers>
    <server>
        <id>registry.hub.docker.com</id>
        <username><DockerHub Username></username>
        <password><DockerHub Password></password>
    </server>
</servers>

There are other ways to provide the credentials, too. The recommended way by Google is to use helper tools, which can store the credentials in an encrypted format in the file system. In this example, we could have used docker-credential-helpers instead of storing plain-text credentials in settings.xml, which is much safer, though simply out of scope for this tutorial.

也有其他的方法来提供凭证。谷歌推荐的方法是使用辅助工具,它可以在文件系统中以加密的格式存储凭证。在这个例子中,我们可以使用docker-credential-helpers,而不是在settings.xml中存储纯文本凭证,这要安全得多,虽然这根本不在本教程的范围之内。

5. Deploying to Docker Hub With Jib

5.使用Jib部署到Docker Hub

Now, we can use jib-maven-plugin, or the Gradle equivalentto containerize our application with a simple command:

现在,我们可以使用jib-maven-plugin,或者Gradle等同物用一个简单的命令将我们的应用程序打包。

mvn compile com.google.cloud.tools:jib-maven-plugin:2.5.0:build -Dimage=$IMAGE_PATH

where IMAGE_PATH is the target path in the container registry.

其中IMAGE_PATH是容器注册表中的目标路径。

For example, to upload the image baeldungjib/spring-jib-app to DockerHub, we would do:

例如,要上传图片baeldungjib/spring-jib-appDockerHub,我们要做的是。

export IMAGE_PATH=registry.hub.docker.com/baeldungjib/spring-jib-app

And that’s it! This will build the docker image of our application and push it to the DockerHub.

就这样了!这将建立我们应用程序的docker镜像,并将其推送到DockerHub

ibDocker 1

We can, of course, upload the image to Google Container Registry or Amazon Elastic Container Registry in a similar way.

我们当然可以, 以类似的方式将图像上传至Google容器注册中心Amazon Elastic容器注册中心

6. Simplifying the Maven Command

6.简化Maven命令

Also, we can shorten our initial command by configuring the plugin in our pom instead, like any other maven plugin.

另外,我们可以像其他maven插件一样,在pom中配置该插件,从而缩短初始命令。

<project>
    ...
    <build>
        <plugins>
            ...
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>2.5.0</version>
                <configuration>
                    <to>
                        <image>${image.path}</image>
                    </to>
                </configuration>
            </plugin>
            ...
        </plugins>
    </build>
    ...
</project>

With this change, we can simplify our maven command:

通过这一改变,我们可以简化我们的maven命令。

mvn compile jib:build

7. Customizing Docker Aspects

7.自定义Docker Aspects

By default, Jib makes a number of reasonable guesses about what we want, like the FROM and the ENTRYPOINT.

默认情况下,Jib对我们想要的东西做了一些合理的猜测,如FROM和ENTRYPOINT。

Let’s make a couple of changes to our application that are more specific to our needs.

让我们对我们的应用程序做一些更具体的修改,以满足我们的需要。

First, Spring Boot exposes port 8080 by default.

首先,Spring Boot默认暴露的端口是8080。

But, let’s say, we want to make our application run on port 8082 and make it exposable through a container.

但是,比方说,我们想让我们的应用程序在8082端口上运行,并通过容器使其可被公开。

Of course, we’ll make the appropriate changes in Boot. And, after that, we can use Jib to make it exposable in the image:

当然,我们会在Boot中做适当的修改。而且,在这之后,我们可以用Jib来使它在图像中可被暴露。

<configuration>
    ...
    <container>
        <ports>
            <port>8082</port>
        </ports>
    </container>
</configuration>

Or, let’s say we need a different FROM. By default, Jib uses the distro-less java image.

或者说,我们需要一个不同的FROM。默认情况下,Jib使用distro-less java图像

If we want to run our application on a different base image, like alpine-java, we can configure it in a similar way:

如果我们想在不同的基础镜像上运行我们的应用程序,比如alpine-java,我们可以用类似的方式进行配置。

<configuration>
    ...
    <from>                           
        <image>openjdk:alpine</image>
    </from>
    ...
</configuration>

We configure tags, volumes, and several other Docker directives in the same way.

我们以同样的方式配置标签、卷和其他几个Docker指令

8. Customizing Java Aspects

8.自定义Java Aspects

And, by association, Jib supports numerous Java runtime configurations, too:

而且,通过关联,Jib也支持许多Java运行时配置。

  • jvmFlags is for indicating what startup flags to pass to the JVM.
  • mainClass is for indicating the main class, which Jib will attempt to infer automatically by default. 
  • args is where we’d specify the program arguments passed to the main method.

Of course, make sure to check out Jib’s documentation to see all the configuration properties available.

当然,请确保查看Jib的文档以了解所有可用的配置属性

9. Conclusion

9.结论

In this tutorial, we saw how to build and publish docker images using Google’s Jib, including how to access the docker directives and Java runtime configurations through Maven.

在本教程中,我们看到如何使用谷歌的Jib构建和发布docker镜像,包括如何通过Maven访问docker指令和Java运行时配置。

As always, the source code for this example is available over on Github.

像往常一样,这个例子的源代码可以在Github上找到