Dockerize your Spring Boot application in 9 steps

Sufyan Khot
4 min readSep 4, 2022

--

Prerequisites:

  1. You must know fundamental docker concepts including images, containers, Docker engine and others.
  2. You must have Docker installed on your system
  3. You must have Docker Compose installed on your system
  4. A Spring Boot project.

Steps:

Step 1: Create a Spring Boot application

You may have your own or you may also refer the following Github repository: https://github.com/Sufi737/employee-application-dockerized

Step 2: Add Docker Maven plugin as a dependency in pom.xml file

We add dockerfile-maven-plugin dependency in our pom.xml file

For the repository name we are using a predefined variable docker.image.prefix for which we will set the value in the next step.

Notice that we are also setting the JAR file location in <JAR_FILE>. We will use this value when we create our Dockerfile in further steps.

Step 3: Set the value for docker.image.prefix

In the pom.xml file again, we can declare this variable as a property. This is the same area in pom.xml where we specify our java.version

Here, I have set the value as “employee”. You may set it the value you want to.

Step 4: Create the Dockerfile

This file contains instructions to create Docker images. The commands in this file are similar to Linux commands. In order to make the instructions in Dockerfile execute, we use the docker build command which creates the images for us.

The location of Dockerfile is the root directory of the project.

In the Dockerfile we will copy our JAR file inside the container and execute it. We will use the JDK image openjdk:11-slim. Note that you may need to use a different version based on your Java version. You can find supported versions here: https://hub.docker.com/_/openjdk

The JAR file is located in the target directory of your project. If it is not present run the following command: mvn clean package

Below is a Dockerfile we can use

But this Dockerfile copies entire JAR which contains unnecessary files as well which are not required for our application to run. To solve this we can use a multi-stage Dockerfile where in one stage we can just copy our JAR file and in the next stage we copy only relevant files in the JAR which are required.

We will copy below directories in our container:

  1. BOOT-INF/lib: Contains all the JARs required including dependencies
  2. BOOT-INF/classes: Contains our custom classes created for our application
  3. META-INF: Contains basic meta info about the project

Below is a Dockerfile that does this

Step 5: Build the Docker image

As our Dockerfile is ready we can build our Docker image using the following command:

mvn package dockerfile:build

If this command is successful, an image will be created which we can see using this command: docker images

Step 6: Create Docker Compose file

Using Docker Compose we can define multiple services as a group. For example, we can define our Spring Boot application, our database, Redis, and so on as a single group and start their containers together. For this we create a docker-compose.yml file.

A sample docker-compose.yml file below

In the services node, we are creating two services: employee-service and db. You may name whatever service name suits your application.

Notice that in the image node for employee-service we are specifying the image name we have created in the previous steps. You can see your image name using the docker images command.

Step 7: Create the SQL intitialization file

Notice that in the docker-compose.yml file, for the db service we have a volumes node where we are mounting sql-scripts directory to docker-entrypoint-initdb.d

The docker-entrypoint-initdb.d is a special directory where .sh, .sql and .sql.gz files are executed during startup. We can place our SQL scripts that we want to run in the sql-scripts directory and then mount it to this directory. For that create a sql-scripts directory in your root application directory and add the .sql file you want. I am using the below schema.sql file.

Step 8: Start the container

We can start our containers using docker-compose up command. Add -d option if you want them to keep running in the background as follows:

docker-compose up -d

Step 9: Verify if everything’s working!

Run docker ps command and you should be able to see two containers running. In my case it is mysql:8.0 and employee/employee:0.0.1-SNAPSHOT

To test if the application is running now we can verify on our browser at http://localhost:8080/

To check whether you SQL scripts are executed, execute the following command:

docker exec -it <container id>/bin/bash

You will get container ID in docker ps command output.

The above command will let you inside the mysql container. Now you can login to MySQL using below command:

mysql -u root -p

It will prompt you for a password, we have specified the password as root in docker-compose.yml file. You may specify a password of your choice

You may verify the database using below MySQL command:

SHOW DATABASES;

It should show the database name you have specified in the docker-compose.yml. In my case it is “employee”.

Run the below MySQL command:

USE <database name>

Example: USE employee;

and then SHOW TABLES;

You should be able to see the tables you had added using the SQL file in the sql-scripts directory

Something extra:

Final notes:

The above steps will help you achieve Dockerization of you Spring Boot application. If you want to stop the running containers you may run the following command:

docker-compose down

In case you are making changes to the .sql file inside sql-scripts directory, you need to stop the running containers using the above command, and then start them again using the docker-compose up command with — force-recreate flag as follows:

docker-compose up -d — force-recreate

--

--

No responses yet