Dockerizing a Java Application – Dockerizing a Java Application

最后修改: 2022年 8月 5日

1. Overview


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


2. Building the Runnable Jar

2. 建立可运行罐

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


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


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:



3. Writing the Dockerfile


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
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 in this case. This step doesn’t create any additional layers.


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


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


mvn package

After that, let’s build our Docker image:


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.


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


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.


4. Conclusion


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上找到。