1. Introduction
Docker is a good option when it comes to making applications deployable across different environments. In this tutorial, we’ll learn how to package our Java applications into a Docker image that can run consistently anywhere.
We’ll use our file-upload application created previously and configure it to build an executable JAR file. Then, we’ll package it into a Docker image.
Let’s get started.
2. Setting Up Our Java Project
For this tutorial, we’ll use Maven to manage our project’s dependencies and build processes.
2.1 Maven Configuration (pom.xml
)
First, let’s configure our pom.xml
file to package the jar file along with all its dependencies. For this, we’ll use the maven-assembly-plugin
added to our plugins under the <build>
section:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.namastecode.SpringBoot3ThymeleafApplication</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Let’s break down the important parts:
<mainClass>
specifies the entrypoint of our application.<descriptorRefs>
configured withjar-with-dependencies
will package our application and all its dependencies into a single JAR file.<execution>
tells Maven to run during thepackage
phase of the Maven lifecycle.
Now, when we run mvn package
, Maven will create two JAR files in the target
directory - one with dependencies and one without. The JAR with dependencies will have a jar-with-dependencies
suffix.
We can test that the application runs correctly using the command:
java -jar target/spring-boot-3-thymeleaf-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Now we’re ready to create a Docker image.
3. Dockerize Java Application
Containers are a great way to package Java applications. They provide a consistent environment for running applications, regardless of where they are deployed. Let’s see how to create a Docker image for our Java application.
3.1 Create Dockerfile
We start by adding a Dockerfile at the root of our project:
spring-boot-3-thymeleaf/
├── src/
│ ├── main/
│ │ └── java/
│ │ └── com/namastecode/
| | | -- Other files
│ │ └── SpringBoot3ThymeleafApplication.java
│ └── test/
│ └── java/
├── pom.xml
└── Dockerfile
A Dockerfile
is simply a text file containing instructions for Docker to assemble an image. Each instruction creates a new immutable layer that builds on the previous one.
We’ll start by defining our base image and the working directory where we want to place our application inside the container. Then, we’ll copy the JAR file and add the instructions to run it:
FROM eclipse-temurin:23-jre-alpine
WORKDIR /app
COPY target/spring-boot-3-thymeleaf-0.0.1-SNAPSHOT-jar-with-dependencies.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
For our base image, we’re using eclipse-temurin:23-jre-alpine
, which is a lightweight version of the JRE (Java Runtime Environment) based on Alpine Linux. This is an ideal choice for running Java applications in a container when no specific dependencies are required.
We define /app
as the working directory inside the container. This is where our application will be copied using the COPY
command. Finally, we set the entrypoint to run our application with the java -jar
command. The entrypoint is the command that will execute when the container starts.
3.2 Create a Docker Image
Now, we run the build
command to create a new image from the root of our project:
docker build -t spring-boot-3-thymeleaf .
After the image is created, we can simply run docker image ls
to verify that the image was built successfully.
3.3 Running the Docker Image
Finally, let’s run our Dockerized Java application:
docker run -p 8090:8090 spring-boot-3-thymeleaf:latest
This will spin up a new container and run our Java application inside it. We should see the logs from your Spring Boot application in the terminal. Also, we are mapping port 8090
on the host to port 8090
on the container, which is the port for our Spring Boot application exposes. To check the application is running, we can visit http://localhost:8090
in our web browser.
4. Conclusion
In this tutorial, we learned how to create a Docker image from a Java application. We started by configuring our Maven project to package the application with its dependencies, then created a Dockerfile to define the image, and finally built and ran the Docker image.
You can find the complete code on GitHub.