How to Dockerize Your Spring Boot Application

Introduction to Dockerfile with Spring Boot – How to Dockerize SpringBoot App

In this tutorial, you will learn step by step how to Dockerize your Spring Boot application. We would create a Docker container of a Spring Boot application and run it on Docker.

In this tutorial, we’ll work without a database. However, in the next part, we’ll see how to incorporate a MySQL database.

Why do you need to Dockerize?

This is simply to ensure that your application can easily be distributed and run anywhere without configuration. So when you dockerize, then you pack your application into an image(kind of OS) can be just start and run.

Since this is a practical tutorial, let’s just get started.

  1. Build the Spring Boot Application
  2. Dockerize and Run it
  3. Using Buidpacks without Dockerfile

 

1. Build a SpringBoot Application

For this application, I have used IntelliJ with Spring Initializer. Only two dependencies are added:

  • Spring Web
  • Lombok

Next, I have created two classes:

  • Person: Just a simple model for person object
  • HomeController:  The restcontroller to serve the application

These two files are shown below.

The Person class

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {

    private String firstname;
    private String lastname;
    private String location;

}

 

The Controller

@RestController
public class HomeController {

    @GetMapping("/")
    private List<Person> personList() {
        ArrayList<Person> personList = new ArrayList<Person>(
                Arrays.asList(
                new Person("Kindson", "Munonye", "Budapest"),
                new Person("Adaku", "Okeke", "Nigeria"),
                new Person("Helen", "Grudals", "Hungary")
        ));
        return personList;
    }

}

 

Now if you run this application it works. You can go to http://localhost:8080/ and you’ll see the list of users.

 

2. Let’s Dockerize and Run!

The first step is to create a Java Archive (jar file) of your application. To do that, run the command in you terminal:

mvn clean install

 

This would generate your .jar file. So if you look into the target folder you’ll see this file as shown below:

Generate jar file from Spring Boot
Generate jar file from Spring Boot

 

Next, create a new file and name it Dockerfile. Note the casing ( it’s not DockerFile or dockerFile!)

Enter the following in this file:

FROM adoptopenjdk/openjdk11:alpine-jre
EXPOSE 8080
ARG JAR_FILE=target/dockertutorial-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

 

Next, build your the docker image of your application using the following command:

docker build -t demoapp .

Before you run this command, ensure that Docker is started on your computer

This command generates an image of your app with the tag demoapp. Also take note of the period(.) at the end of the line. This tells docker to use a Dockerfile in the same directory.

You can now check that the image is created using the command:

docker image ls

 

Finally, to run the image, use the command:

docker run -p8080:8080 demoapp

 

The application starts up at port 8080 and you can access it as before.

 

3. Using BuildPacks Without Dockerfile

Buildpack is a new feature introduces in Spring Boot 2.3.0.M1 to improve containerization. With this, you don’t need a docker file. Buildpacks takes your application and converts it to something that can run in docker.

So to generate docker image using Buildpacks, you don’t need to create a Dockerfile. Just run the command below and your image is generated:

mvn spring-boot:build-image

Again, you can run the container with:

docker run -p8080:8080 image-name:tag-name

You can find the image-name and tag-name using docker image ls as before.

Admin bar avatar

kindsonthegenius

Kindson Munonye is currently completing his doctoral program in Software Engineering in Budapest University of Technology and Economics

View all posts by kindsonthegenius →

Leave a Reply

Your email address will not be published. Required fields are marked *