Dockerize a Spring Boot Application

Dockerize a Spring Boot Application

Table of Contents

Starting with this tutorial, I will go through a deployment series of a Java application to various cloud providers.

Before setting up a pipeline and walking you through the deployment process, I want to dockerize an application so we can use this in the deployment process.

What is Docker, and why do we need to Dockerize an application?

Docker is a Linux-based container management tool that enables you to create, publish and consume container images.

Containerization of an application with Docker will create a consistent environment in recreating applications and deploying them to the different cloud providers. That commits write once, run anywhere. 

  • Containerized apps start and stop very quickly.
  • Docker helps efficiency when it comes to the usage of system resources
  • Ability to run on different platforms & cloud providers
  • Isolation between host system and application
  • Easy to scale

I’m writing this tutorial with the prerequisites below;

  • You have Docker installed, up and running!

Dockerize a Spring Boot Application

Initialize a Spring Boot Project

I have already created an elementary Spring Boot application for this tutorial. It provides a simple GET endpoint with /ping path and returns “pong” as a response. You can clone from Exceptionly Github account or generate a Spring Boot application with Spring Initializr

After we clone the application, build executable jar file by Gradle build task on the left Gradle UI or with ./gradlew build command from the terminal.

Containerize Application With Docker

Docker is building images automatically by executing the instructions from a Dockerfile. 

Official Docker Docs statement for Dockerfile:

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

Create the following Dockerfile in the application root folder

				
					FROM eclipse-temurin:17
COPY build/libs/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
				
			

Eclipse Temurin is providing the official OpenJDK binary images. In the above example, we are using Java 17 version. 

Find the Eclipse Temurin images in the Docker hub

This docker build does the following:

  1. It starts from the Eclipse Temurin official OpenJDK image for Java 17
  2. Copies the Java executable jar inside the container
  3. Configures the container for the instructions in the ENTRYPOINT

Run this Dockerfile with the command to build an image;

				
					$ docker build -t exceptionly/demo .
				
			

This command will build an image named exceptionly/demo from the Dockerfile.

Verify built image; 

				
					$ docker images
REPOSITORY         TAG       IMAGE ID       CREATED          SIZE
exceptionly/demo  latest    bd276cd342d0   15 minutes ago   480MB
				
			

Run the dockerized application container with the following command:

-p binds port 8080 of the container to TCP port 8080 on localhost

				
					$ docker run -p 8080:8080 exceptionly/demo

:: Spring Boot ::                (v2.6.0)
Starting DemoApplication using Java 17.0.1 on 755fff7c521e with PID 1 (/app.jar started by root in /)
…
Started DemoApplication in 4.249 seconds (JVM running for 4.95)

				
			

List the containers are running currently:

				
					
$ docker ps
c13124bf328d   exceptionly/demo:latest   "java -jar /app.jar"   5 seconds ago   Up 4 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp 
				
			

The Spring Boot application is up and running in a Docker container. Navigate localhost:8080/ping endpoint in the browser or curl localhost:8080/ping from the command line.

				
					$ curl http://localhost:8080/ping
pong
				
			

As always being said; The best way to learn a tool or language is to practice.

Try to dockerize an application that has a different tech stack. Feel free to check out my previous posts to initialize an application if you haven’t yet!

4 Responses

  1. تنبيه: Exceptionly

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

arArabic