Dockerizing a Java Application – Dockerizing a Java Application

最后修改: 2022年 8月 5日

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

1. Overview

1.概述

In this article, we’ll show how to Dockerize a Java runnable jar-based application. Do read about the benefits of using Docker.

在这篇文章中,我们将展示如何对基于Java可运行的jar应用程序进行Dockerize。请阅读有关使用Docker的好处

2. Building the Runnable Jar

2. 建立可运行罐

We’ll be using Maven to build a runnable jar.

我们将使用Maven来构建一个可运行的jar

So, our application has a simple class, HelloWorld.java, with a main method:

因此,我们的应用程序有一个简单的类,HelloWorld.java,有一个main方法。

public class HelloWorld {
    public static void main(String[] args){
        System.out.println("Welcome to our application");
    }
}

And we’re using the maven-jar-plugin to generate a runnable jar:

我们使用maven-jar-plugin来生成一个可运行的jar。

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
     <version>${maven-jar-plugin.version}</version>             
      <configuration>
          <archive>
             <manifest>
              <mainClass>com.baeldung.HelloWorld</mainClass>
              </manifest>
           </archive>
       </configuration>
</plugin>

3. Writing the Dockerfile

3.编写Docker文件

Let’s write the steps to Dockerize our runnable jar in a Dockerfile. The Dockerfile resides in the root directory of the build context:

让我们在Dockerfile中写下将我们的可运行jar文件Docker化的步骤。Dockerfile驻留在build context的根目录下。

FROM openjdk:11
MAINTAINER baeldung.com
COPY target/docker-java-jar-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Here, in the first line, we’re importing the OpenJDK Java version 11 image as our base image from their official repository. Subsequent lines will create additional layers over this base image as we advance.

在这里,在第一行,我们从他们的官方仓库导入OpenJDK Java 11版本的镜像作为我们的基础镜像。随后的几行将在这个基础镜像上创建额外的层,随着我们的推进

In the second line, we specify the maintainer for our image, which is baeldung.com in this case. This step doesn’t create any additional layers.

在第二行,我们指定图像的维护者,在本例中是baeldung.com。这一步并不创建任何额外的层。

In the third line, we create a new layer by copying the generated jar, docker-java-jar-0.0.1-SNAPSHOT.jar, from the target folder of the build context into the root folder of our container with the name app.jar.

在第三行,我们创建一个新的层,将生成的jar,docker-java-jar-0.0.1-SNAPSHOT.jar,从build contexttarget文件夹复制到我们容器的root文件夹,名称为app.jar

And in the final line, we specify the main application with the unified command that gets executed for this image. In this case, we tell the container to run the app.jar using the java -jar command. Also, this line does not introduce any additional layer.

而在最后一行,我们用为这个镜像执行的统一命令指定主应用程序。在这种情况下,我们告诉容器使用app.jar命令来运行java -jar。另外,这一行并没有引入任何额外的层。

4. Building and Testing the Image

4.构建和测试图像

Now that we have our Dockerfile, let’s use Maven to build and package our runnable jar:

现在我们有了Dockerfile,让我们用Maven来构建和打包我们的可运行jar。

mvn package

After that, let’s build our Docker image:

之后,让我们建立我们的Docker镜像。

docker image build -t docker-java-jar:latest .

Here, we use the -t flag to specify a name and tag in <name>:<tag> format. In this case, docker-java-jar is our image name, and the tag is latest. The “.” signifies the path where our Dockerfile resides. In this example, it’s simply the current directory.

在这里,我们使用-t标志来指定<name>:<tag>格式的名称和标签。在这个例子中,docker-java-jar是我们的图像名称,而标签是latest。”. “表示我们的Dockerfile所在的路径。在这个例子中,它只是当前目录。

Note: We can build different Docker images with the same name and different tags.

注意:我们可以用相同的名字和不同的标签建立不同的Docker镜像。

Finally, let’s run our Docker image from the command line:

最后,让我们从命令行中运行我们的Docker镜像。

docker run docker-java-jar:latest

The above command runs our Docker image, identifying it by the name and the tag in the <name>:<tag> format.

上述命令运行我们的Docker镜像,以<name>:<tag>格式识别它的名称和标签。

4. Conclusion

4.总结

In this article, we’ve seen steps involved in Dockerizing a runnable Java jar. The code sample used in this article is available over on GitHub.

在这篇文章中,我们看到了对可运行的Java jar进行Docker化的步骤。本文中使用的代码样本可在GitHub上找到。