Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

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

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

So, our application has a simple class, HelloWorld.java, 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:

<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

Let's write the steps to Dockerize our runnable jar in a Dockerfile. The Dockerfile resides in the root directory of the 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.

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.

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.

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.

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.

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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!