Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:


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,, 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:

FROM openjdk:17-jdk-alpine
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 17 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 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.

Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:


res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.