Dockerizing a Spring Boot Application and Using the Jib Maven Plugin

Betül Şahin
8 min readNov 23, 2021

--

In this tutorial, we’ll talk about dockerizing the Spring Boot applications using dockerfile and docker tool. Then we’ll also publish the Docker image to dockerhub.

And then we’ll be doing all of these steps with the help of Maven Jib Plugin.

Here, I shared what I learned in the Java & Spring Framework Bootcamp organized in collaboration with GittiGidiyorKodluyoruz & Patika.dev, which I attended this summer as a student.

What is Docker?

Wikipedia defines Docker as

Docker can package an application and its dependencies in a virtual container that can run on any Linux, Windows, or macOS computer. This enables the application to run in a variety of locations, such as on-premises, in a public cloud, and/or in a private cloud. When running on Linux, Docker uses the resource isolation features of the Linux kernel (such as cgroups and kernel namespaces) and a union-capable file system (such as OverlayFS) to allow containers to run within a single Linux instance, avoiding the overhead of starting and maintaining virtual machines. Docker on macOS uses a Linux virtual machine to run the containers.

Because Docker containers are lightweight, a single server or virtual machine can run several containers simultaneously. A 2018 analysis found that a typical Docker use case involves running eight containers per host, and that a quarter of analyzed organizations run 18 or more per host.

To summarize briefly, we can use PostgreSql database via Docker in our application without installing a PostgreSql database on our computer . With Docker, we can up our entire system with a single command.

What is the difference between Image and Container ?

Let’s clarify this with an example. If we think of Docker images as a java classes, the container corresponds to the instances we produce with the new operator from these classes.

What is Dockerhub ?

Where we host our Docker images. For example, github where we host our projects that we versioned with git.

Docker Installation

For installation, you can download the appropriate one for your operating system from that address. Once the installation is complete, you will see the following screen. If the lower left corner is green, it is working successfully. (it can take a long time to turn green on Windows computers.)

Common Docker Commands

Here are a few of the basic commands from the Docker CLI reference we often use.

  • docker — lists Docker commands from terminal.
C:\springboot-docker-maven-jib-plugin> docker
  • docker help — You can also access the same list with the following command.
C:\springboot-docker-maven-jib-plugin> docker --help

If you want to learn the details on any command, you can use this command as follows. For example, let’s learn the details of the update command.

C:\springboot-docker-maven-jib-plugin> docker update --help
  • docker image — lists all images.
C:\springboot-docker-maven-jib-plugin> docker image ls

or

C:\springboot-docker-maven-jib-plugin> docker images
  • docker rmi — removes an image using its short ID( first 3–4 characters).
| REPOSITORY  | TAG    | IMAGE ID     | CREATED     | SIZE   |
| ----------- | ------ | ------------ | ----------- | ------ |
| hello-world | latest | feb5d9fea6a5 | 4 weeks ago | 13.3kB |
C:\springboot-docker-maven-jib-plugin> docker rmi feb5
  • docker run — runs an image from Dockerhub.
C:\springboot-docker-maven-jib-plugin> docker run hello-world
  • docker pull — pulls an image from Dockerhub.
C:\springboot-docker-maven-jib-plugin> docker pull hello-world
  • docker container — shows all containers with inactive containers that have run and died.
C:\springboot-docker-maven-jib-plugin> docker container ls -a

This command lists all running containers.

C:\springboot-docker-maven-jib-plugin> docker container ls
  • docker ps — Another command to list running docker containers. we can see the inactive containers by adding “-a” to the end of this command.
C:\springboot-docker-maven-jib-plugin> docker ps
  • docker stop — stops the running container.
C:\springboot-docker-maven-jib-plugin> docker stop <CONTAINER_ID>
  • docker rm — removes the container.
C:\springboot-docker-maven-jib-plugin> docker rm <CONTAINER_ID>
  • prune — removes all stopped containers with a single command.
C:\springboot-docker-maven-jib-plugin> docker container prune

For other docker commands, you can continue from this reference.

Dockerize a Spring Boot Application

I built a simple Restfull application using Spring Boot to get some data from the PostgreSql database. There is an API in the project that we can add/list products with it. You can access the sample project at this address.

Docker has a configuration file called Dockerfile. This file allows you to create a new docker image. So we containerize with it.

Creating a Dockerfile

We create the following Dockerfile in the main folder of the project.

  • FROM — We need the JDK for the Spring application to run. For this, we use OpenJDK, which is the open source implementation of the Java platform.
  • EXPOSE — tells which port it will run on.
  • ADD — naming the jar file. Renames the jar file under target directory.
  • ENTRYPOINT — is to set the image’s main command, allowing that image to be run as though it was that command. So it makes it do something as executable.

After creating this file, we run the following command to dockerize our application.

docker build -t spring-docker-demo .

Here we write the tag name after “-t”. (We can name whatever we want.)

“.” The dot at the end shows that it will put the image it has built in the folder it is in.

You will get an output like below. So we created the docker image of our Spring application!

We use the following command to run the image we created locally.

docker run -p 8080:8080 spring-docker-demo

Here, the second part of “8080:8080 is the port where we will run the application locally. For example, “8080:9090 shows us to our application will run at “localhost:9090”.

Did you notice that we haven’t done the PostgreSql configurations of our application until now. So when you run the container you might receive the following errors. We’ll fix it later.

Uploading to Dockerhub

We provide public access by uploading the docker image we created to dockerhub. Anyone can get access to free public repositories for storing and sharing images.

First you can create a free account here. Then you need to login by running the following command from the terminal.

docker login

We tag the image name according to our dockerhub username.

docker tag spring-docker-demo betulsahinn/spring-docker-demo

Then we upload the image to dockerhub with the following push command.

docker push betulsahinn/spring-docker-demo

We can pull the uploaded image to our local with the pull command. Or with the run command, we can pull it to our local and run it at the same time.

docker pull betulsahinn/spring-docker-demodocker run betulsahinn/spring-docker-demo

Creating a docker-compose.yml

This file allows us to run multiple containers simultaneously. For detailed information, you can go to the here.

We create the following file named docker-compose.yml in the main folder of the project. It contains keywords backend and PostgreSQL, which defines services for web and database portions of the application.

Then we start the project with the following command.

docker-compose -f docker-compose.yml up -d

You don’t need to create a database, it creates it automatically. Isn’t it very good?

After following the steps listed in the tutorial, you will reach the swagger ui at localhost:8080/swagger-ui.html. As you can see, we run the entire system with a single command without the need for any installation.

Using Jib Maven Plugin

Now we will simplifies containerization of Spring application using Jib, which is a maven plugin. We’ll build its docker image using jib. And then we’ll also upload the image to Dockerhub using Jib.

First, put the following jib maven plugin declaration in the <plugins></plugins> section of your pom.xml file.

We create our container with the above declaration.

In the <configuration></configuration> section, we have configured the docker image at the basic level. For detailed information, you can check the jib official page.

Then we use the following command to build the docker image that we declared with the jib plugin.

mvn clean install jib:dockerBuild -Djib.to.image=spring-docker-demo:v1

After running this command, you may get the following warnings. Run the same command again.

You can run the docker image we created with Jib at localhost:8080 by typing the following commands in the terminal.

docker run -p 8080:8080 spring-docker-demo:v1

Secondly, let’s push the image to dockerhub with jib commands. While tagging here, write your dockerhub username and the version.

mvn clean install jib:build -Djib.to.image=betulsahinn/spring-docker-demo:v1
  • If you get a warning, run the same command again.

Now, let’s simplfy the dockerhub push with this command using jib plugins.

First of all, let’s make the following additions to the properties section of the pom.xml file.

<properties>
<java.version>1.8</java.version>
<app.image.name>spring-docker-demo</app.image.name>
<app.image.tag/>

</properties>

Then we define the profile in the pom.xml file. We add the following declaration after closing the </plugins></build> tag.

The first profile is for pushing to dockerhub and the second profile is for pushing to locale.

From the Maven section, mark the profiles as in the second picture below and run the command. We did not give a tag name to the application because we defined it in the properties section. We just need to give the version.

mvn clean install -P jib-push-to-dockerhub -Dapp.image.tag=v2

Then if you check your dockerhub account you will see that it is uploaded there.

To run without using jib commands, select profiles from the maven section and run mvn clean install command.

Do not forget to add your dockerhub username to the beginning of the image name to run the project you sent to Dockerhub locally.

docker run -p 8080:8080 betulsahinn/spring-docker-demo:v1

--

--