How to dockerize Spring Boot apps

Spring Boot is an opinionated, cloud-native, software development framework that's purpose-built to create microservices and 12-factor apps. In other words, Spring Boot encourages container-based packaging and deployment.

While many containerization technologies exist, including Podman and containerd, Docker is the industry's most popular. That's why every cloud-native developer must know how to containerize and dockerize Spring Boot applications.

How to dockerize a Spring Boot app

To dockerize a Spring Boot application, follow these steps:

  1. Set your Spring Boot project's packaging type to Jar.
  2. Build your Spring Boot app with Maven or Gradle.
  3. Add a Spring Boot Dockerfile to the root of your project.
  4. Run the docker build command to create the Spring Boot Docker image.
  5. Run the docker push command to move the image to your container registry.

Select Jar as the project's packaging type

Make sure your Spring Boot project's packaging type is set to Jar. It's possible to containerize War files, but it requires extra configuration.

When a Spring Boot app is packaged as a Jar file, an embedded Tomcat server is included in the build, which eliminates the need to additionally provision an application server.

Docker Hub container registry
A dockerized Spring Boot app can be pushed to a container registry like Docker Hub.

Build and package your Spring Boot application

Both Maven and Gradle include build steps that compile, test and package Spring Boot applications. With Maven, it's as simple as issuing the following command:

mvn package

After the command runs, look in the project's target folder and ensure a Spring Boot Jar file has been created. Copy the name of the Jar file, as it will be referenced in the next step where we create the Spring Boot Dockerfile.

Run your Spring Boot Jar

If you want to test your application prior to containerization, run the Jar by substituting the name of your file into the following command:

java -jar target/roshambo-1.0.jar
Where to find the Spring Boot Jar file
The Spring Boot Jar file to dockerize is found in the target folder. The Dockerfile, created in the next step, goes in the root of the project.

Create a Spring Boot Dockerfile

A Dockerfile describes the runtime resources required to support your application, along with information on how to copy your Spring Boot application into a Docker image. At a minimum, a Spring Boot Dockerfile includes the following:

  • The Java distribution and JDK version to use.
  • How to copy your Spring Boot Jar file into a Docker image.
  • The entry point command to run to start your Spring Boot app.

Here is a sample Spring Boot Dockerfile that includes an additional maintainer label:

FROM openjdk:17-jdk-alpine
LABEL maintainer=mcnz.com
COPY target/roshambo.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

This Spring Boot Dockerfile uses the OpenJDK distribution and version 17 of the JDK. There are many other Java distributions from which to choose, including JDKs from Amazon Corretto, Temurin and Oracle.

Save the Dockerfile in the root of your Spring Boot project. The Dockerfile is extensionless, so ensure your IDE does not add an extension such as .txt or .html.

Build the Spring Boot Docker image

In the same folder as the Dockerfile, run the docker build command.

Include a tag that references your Docker Hub account if that is your target container registry. This helps Docker Hub associate your image with your account when you push to the cloud.

The docker build command

A generic version of the command is as follows:

docker build –tag=<dockerhubname>/<imagename>:<revision> .

I used the following command to push my dockerized Spring Boot app to Docker Hub:

docker build --tag=cameronmcnz/roshambo:latest .

Do not forget the period at the end of the statement. This instructs the command to look for the Dockerfile in the current folder.

The build command to dockerize a Spring Boot app
When you dockerize your Spring Boot app, tag it with the name of your Docker Hub account.

Push the dockerized Spring Boot app to Docker Hub

The target destination for most Docker images is Docker Hub, although any artifact repository can be configured to host images.

To push the dockerized Spring Boot app to Docker Hub, simply log in and perform a push. To push the Spring Boot image to my Docker Hub account, I issued the following two commands:

docker login
docker push cameronmcnz/roshambo:latest

Once the command completes, go to your personal Docker Hub account and you'll see the new image listed as recently pushed.

Spring Boot microservice packaging options
Package Spring Boot apps as a Jar to embed Tomcat in the deployment artifact.

Run the containerized Spring Boot app

To run the dockerized Spring Boot app, simply issue a docker run command and provide a port mapping:

docker run –p 80:8080 cameronmcnz/roshambo

The above command runs the published Docker container on your local machine, with the application available in the browser on port 80.

Simplified lifecycle management through dockerization

Container-based packaging plays a pivotal role in the deployment and runtime management of microservices and cloud-native apps. Once you've dockerized a Spring Boot app, you'll find this greatly simplifies many aspects of the software development lifecycle, from testing to deployment.

Cameron McKenzie has been a Java EE software engineer for 20 years. His current specialties include Agile development; DevOps; and container-based technologies such as Docker, Swarm and Kubernetes.

View All Videos
App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close