avatarStanislav Vain

Summary

This context provides a detailed guide on how to Dockerize a Spring Boot application using the docker-maven-plugin.

Abstract

The context begins by explaining the popularity and benefits of using Docker, such as microservice architecture, CI/CD, fast scalability, and ease of use. It then proceeds to outline the prerequisites for creating a Spring Boot application, including JDK 11, Spring Boot 2, Maven 3.2+, and Docker. The guide then walks through the process of setting up a Spring Boot application, creating a Dockerfile, and building and running a Docker container. The context also introduces the docker-maven-plugin, which integrates Docker into the Maven lifecycle, making it easier to manage Docker images and containers. The plugin is added to the pom.xml file and supports various commands, such as docker:start, docker:stop, docker:build, and docker:push. The context concludes by summarizing the benefits of using the docker-maven-plugin and providing links to the source code and user manual.

Bullet points

  • Docker is a popular tool for building and running applications using containerization.
  • Benefits of using Docker include microservice architecture, CI/CD, fast scalability, and ease of use.
  • Prerequisites for creating a Spring Boot application include JDK 11, Spring Boot 2, Maven 3.2+, and Docker.
  • The guide walks through the process of setting up a Spring Boot application, creating a Dockerfile, and building and running a Docker container.
  • The docker-maven-plugin integrates Docker into the Maven lifecycle, making it easier to manage Docker images and containers.
  • The plugin supports various commands, such as docker:start, docker:stop, docker:build, and docker:push.
  • The context concludes by summarizing the benefits of using the docker-maven-plugin and providing links to the source code and user manual.

Spring Boot with Docker

How to Dockerize a Spring Boot Application with docker-maven-plugin

Photo by frank mckenna on Unsplash

Why Docker?

You may have noticed that Docker is quite popular nowadays. Here’s a Google Trends graph of searches for the term “docker”.

Google Trends Graph

Why is Docker so popular, and why the rise of containers?

Docker is a tool for building applications and providing a runtime environment using the concept of containerization. It opens up new development and deployment opportunities, making life much easier for developers and Ops teams. There are several main reasons for this:

  • Microservice architecture — docker solves the problem of running multiple microservices on the same virtual machine by running different containers for each microservice.
  • CI/CD — сontainers have isolated disk space and include all required dependencies (self-contained)
  • Fast scalability — the container orchestration system can automatically launch new containers when the load increases or decommission containers that for any reason stop working correctly.
  • Ease of use it is an open-source technology with a lot of documentation and examples.

In this article, we will see that creating a container for a Spring Boot application is quite simple.

Spring Boot with Docker

Prerequisites

Create a Spring Boot Application

The best way to create an application is to use Spring Initializr. This tool makes it possible to create a ready-to-use application with all the necessary dependencies. Let’s create a simple application that we will dockerize later.

https://start.spring.io/

I choose the Java 11, Maven Project. We also add a couple of dependencies to create basic functionality for testing.

Set up a Spring Boot Application

We now have an application ready to use. Let’s add a simple REST controller to test the application.

This code returns the current time as a string. Now we can run the app with this command:

mvn spring-boot:run
Test the app

Dockerfile

It’s time to containerize it. To do this, we use a Dockerfile that has a straightforward format. Let’s create a new file in the root of the Spring Boot application with the name Dockerfile

  • de>FROM this keyword shows based on which image to create a container. In our case, we use openjdk:14-alpine. You can explore all possible images in the dockerhub.
  • de>ARG— the ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the --build-arg <varname>=<value> flag.
  • de>COPY — this operation copies the specified file or directory to the container
  • de>CMD — the main purpose of a CMD is to provide defaults for an executing container

To check our container, let’s use a terminal. At this point, docker should be installed and running. We need to create a .war file that will later be placed in the container. To do this, run the following command

mvn clean install

After executing this command, we will have a .war file ready for containerization.

Now we can build a docker image. We give this image a name spring-boot-demo. Let’s go to our application directory and execute the command:

docker build -t spring-boot-demo .

build an image

When you do this for the first time, the process may take several minutes, as the docker downloads all the necessary files. The process will be much faster next time.

We’re ready to run our container with the following command:

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

By default, Spring Boot applications run on port 8080 inside the container, and we mapped that to the same port on the host by using -p on the command line.

run a container

To make sure that the container is working correctly, we should find out its IP. For Windows, Mac, and Linux IP will be different.

docker IP

For example, you can see the IP when starting the docker terminal in Windows. Also, you can try using the following command:

docker-machine ip

Since docker does not work natively on all operating systems, you should clarify the IP address for your system.

We have created a Docker container for a Spring Boot application. Now we can test it. You can see that we no longer use localhost but use docker host.

docker-maven-plugin

A docker-maven-plugin comes into play that makes docker management even easier. With this plugin, we integrate docker into the maven life cycle. The plugin should be added to our pom.xml file in the build tag.

Let’s take a look at some tags.

  • The <name> tag — this tag is used to specify the name of the image.
  • The <contextDir> tag — specifies a directory containing a Dockerfile that will be used to create the image.

In case you don’t want to use a docker file, you can specify all the necessary settings in the <build> tag. In this case, instead of the<contextDir> tag, insert the following code.

To automate the creation of the image, let’s add the following code after the<configuration> tag.

Each time the mvn package command is executed, a docker image will be created.

This plugin also supports configuration via a docker-compose file, especially for running containers specified in docker-compose.yml. Docker Compose handling is also available as an external configuration provider.

You can read the documentation here — https://dmp.fabric8.io/#docker-compose

Run a Docker Container with Maven

After we added the io.fabric8 plugin to the pom.xml file, we can use the new commands:

mvn package

Time to test everything together. Let’s create .war file and build the docker image with the command:

mvn package

We can start our container with the command:

mvn docker:start

If you want to connect to the container to see the logs use the command

mvn docker:logs

Great, now we’re ready. With the help of Maven, you can control the life cycle of the container.

Summary

In this article, we saw two examples of how you can containerize a Spring Boot application. The first option is to use a Dockerfile. The second option is a docker-maven-plugin that does all the work for us. This plugin helps you with building Docker images and managing containers, especially for integration tests.

You can find the source code of the application in the GitHub repository

Java
Spring Boot
Docker
Maven Plugin
Containers
Recommended from ReadMedium