Dockerizing a Java Application

Dockerizing a Java Application

In this articile I’ll show you how dockerizing a Java application can be straightforward and how Docker makes it much easier to develop and deploy cross-platform applications. If you are not familiar with Docker I highly suggest you to check out this article before jumping into this one. Now let’s dive into the process.

Building the project

For this project we’ll be using Spring Boot, you can use Spring Initializr or any other tool to generate the project.

Project is based on the parent:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.1.0</version>
</parent>

Dependency:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Plugin for building the jar:

<build>
   <plugins>
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
   </plugins>
</build>

Now let’s create a Controller, which will expose a simple to endpoint to fetch a String.

@RestController
public class HomeController {

	@GetMapping("/docker-java")
	public String getMessage() {
		return "Running";
	}

}

Now if we run the app and try to reach http://localhost:8080/docker-java we should get the message that we set in the controller.

Creating the Dockerfile

A Dockerfile is a text file that includes instructions for building a Docker image. Create the Dockerfile in your project’s root directory:

FROM maven:3.9.4-amazoncorretto-17-debian AS build
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn package

FROM openjdk:17-jdk-slim
WORKDIR /app
EXPOSE 8080
COPY --from=build app/target/docker-java-0.0.1-SNAPSHOT.jar docker-java.jar
CMD ["java", "-jar", "docker-java.jar"]

As you can see above, we split our docker configuration in two parts. In the first part we use as base image a maven version running amazoncorretto 17 and we also set an alias that we’ll use later in the next part to refer to the output of this part. Using the command WORKDIR we set the base path, then using the command COPY we copy our pom.xml and then we download the dependencies. Again with the command COPY we copy our project’s source code and eventually we build the application using mvn package. It’s important to note that first we download the dependencies and then we package the app. It’s split because we’re leveraing Docker cache. Docker will download the dependencies only the first time and when they change, and since they change rarely we’ll skip this step most of the time.

In the second part we use as base image an openjdk 17, but this time we’re using a slim version. It’s not recommended using the standard version since it meant for development purproses and it will increase our application’s bundle size. Next, we use again the WORKDIR command to set the base path, and then we use the EXPOSE command to expose our application’s port to the outside. After that, by referring to the alias we set up earlier to our build, we use the command COPY to copy our previous generated jar to our base path and we also rename it. Lastly we use the command CMD to execute our application.

Building the image

Now that we have our Dockerfile it’s time to build our project’s image. Run the following command:

docker build -t docker-java .

With the above command we tell Docker to create an image from our Dockerfile and with the parameter -t we also specify a tag.

Running the container

The last step is running our application, and we can easly do it by running this command:

docker run -p 8080:8080 -d docker-java

In our Dockerfile we exposed the port 8080 and now we’re mapping it to our OS port 8080 to make it reachable. With the parameter -d we’re starting our container in detech mode, which means that the Docker container runs in the background of your terminal and it doesn’t receive input or display output.

Testing the application

Now that our container is running we can finally test our application and see if it works properly. Open your browser and type the following url: http://localhost:8080/docker-java.

Conclusion

In this article we have seen all the required steps for dockerizing a java application and how Docker is a great tool to test and deploy applications. The source code of this project is on GitHub.

Lorenzo Miscoli

Software Developer specialized in creating and designing web applications. I have always loved technology and dreamed of working in the IT world, to make full use of my creativity and realize my ideas.
Scroll to Top