Dockerizing a Spring Boot Application

Yiğitcan Nalcı
Javarevisited
Published in
5 min readMay 8, 2020

--

Greetings after a long break 😄, I finally got some time to explain how to dockerize a Spring Boot application.

As you know, I wrote this article first: “A Brief Information About Docker“. It’s better to read this article first because it contains brief information that every developer should know 💻.

As soon as you read the previous post, you are ready to learn to use Docker with Spring Boot.

From this point on, I will assume that you have installed Docker on your own computer.

In this tutorial, I will tell you about how to use Docker with Spring Boot and give an example using Spring Boot and Docker.

  • Spring Boot and Spring Data projects which is used for developing REST APIs are used here.
  • Maven is used to automating the build process.
  • A typical domain-driven development approach is used, separating model, repository, service, and controller classes.

You can download the project source code from this GitHub link.

Technologies and Tools

Library Information System which is developed in this tutorial is dockerized in this post. For more details about this project, you can click this link.

The tutorial here uses IntelliJ IDEA, Maven, and Docker. I’m running on Windows 10 using PowerShell.

All software is written in Java, with Spring 5.2 and Spring Boot 2.2.2, in-memory H2 database is used for persistence.

From here, I’ll assume you have JDK 13.0, IntelliJ, Maven, and Docker installed.

My goal here is to show how to use Docker in a simple way without shuffling the business logic.

A Spring Boot application can be dockerized with two most commonly used approaches.

  • Dockerfile: It contains native Docker commands to build the image.
  • Maven: It has a plugin to build the image.

Dockerizing using Dockerfile

“A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image”. A user can write commands to build an image on Dockerfile.

Our sample Dockerfile is shown below.

Dockerfile

OurDockerfile has four commands. 4️⃣

  • FROM command is used for setting a base image. In our Dockerfile , it isopenjdk:13-jdk-alpine .
  • ARG command is used for passing a defined variable at build-time to the builder. In this case, it is our .jar file.
  • COPY command copies specified file to the image. In this case. our created .jar file will be copied to the base image.
  • ENTRYPOINT is used for how to execute the application inside the container.

After preparing Dockerfile , maven package command should be run to create .jarfile. Because Dockerfile needs this .jar file when running. 💻

Build Result

After all things done, you can run or debug the application using Dockerfile in IntelliJ. I used IntelliJ but if you want, you can do these steps manually. For that, docker build and docker run commands should be used. You can click this link for further information.

You can see the running image and container of our application. 😃

Docker image
Docker container

Dockerizing with Maven

A Spring Boot application also can be dockerized using Maven. There are a few plugin for dockerizing the application.

One of them is the Spotify plugin. 🎵 For this post, this plugin is used. You can see the configuration of our Maven plugin below. This plugin should be added between <build> </build> tags.

pom.xml

As you can see, <baseImage> and <entryPoint> tags are used. These tags have the same purpose of Dockerfile’s FROM and ENTRYPOINTcommands respectively.

After this configuration is done, we can should run the below command to create of an image of our application.

mvn clean package docker:build

Now, created docker image can be shown with below command.

Docker image

After that, you should use docker run ... command to run this image.🏃

Created container for this application looks like below.

Docker container

Testing with Postman

You can easily run this project to testing the REST service.

Of course, you should run the created docker images for testing the REST service. 😃

If you want to add the book to the Library Information System, you can use the data.json under the project root.

After running the project, you can test the REST service. For this, open Postman, enter the URL, and select the GET method like below.

Book list

Again, enter the link with a book name in the database.

Frankenstein

You can add a book to the system with POST method in Postman. First, you should enter Content-Type : application/json header key-value pair. Second, in body tab, you must enter the book information that you want to save the database in JSON format.

Adding a book

If you want to delete a book, you should select DELETE method in Postman and add a book name that you want to delete to the end of the link.

Deleting a book

Note: This tutorial focuses on the how-to dockerize a Spring Boot application in a simple way.

--

--

Yiğitcan Nalcı
Javarevisited

Dad. Software Engineer. Sports lover, doer. Curious about science and technology. Novice writer for now. https://www.linkedin.com/in/yigitcannalci