blog/engineering/java/how to dockerize a Spring Boot application

how to dockerize a Spring Boot application

4 min readpublished 26 August 2022
ImageImage

In this quick tutorial we will learn how to dockerize a Spring Boot application using Docker.

You’ll need:

  • JDK 11 for compiling the project
  • Docker Engine to create Spring Boot containers
  • Any cmd bash to run some commands (Git Bash is optional)
  • Apache Maven to run some commands to compile the project
  • Your IDE (I recommend IntelliJ IDEA)

Download the repository

First off, we are going to download my pre-made repository on Github with all the required dependencies to save some time.

Check the project

Open the downloaded project in your IntelliJ IDEA and run the command mvn clean compile to check if the project is working.

A screenshot of checking a project when dockerizing a Spring Boot application

Check the application

We will check if our Spring Boot application is also running. Let’s run the next command in the internal terminal in IntelliJ (run it from your cmd or Git Bash in Windows):

mvn clean spring-boot:run

curl -v localhost:8080/api/hello

You should see the following message in your cmd to validate that the app is already running and returning the proper message in this route:

A screenshot of checking if the Spring Boot application is running before dockerizing it

Add configuration to dockerize the Spring Boot application

Now it’s time to create the configuration to dockerize our Spring Boot application using Docker.

Just to make it clear, Docker is a platform that combines your application, its dependencies, and even an OS into an image that can run on any platform.

Now we will create a Dockerfile to add all the configurations to dockerize our Spring Boot application.

Create a Dockerfile and add the next configurations (Docker will read as a pipeline to apply them):

Now I will explain a little bit about these commands:

  • FROM creates a layer from an existing Spring Boot Docker image that exists locally or in any container registry. openjdk:11 will be the one to use.
  • VOLUME creates a specific space to persist some data in your container. The tmp folder will store information.
  • EXPOSE informs Docker that the container listens to the specified network ports at runtime. This is the port to access to the Spring Boot container and will be used to run the container.
  • ARG defines a variable that can be passed to the application at runtime. For example, we pass the location of the final jar file within the target folder and save it in a JAR_FILE variable. You can also pass more arguments like credentials, keys, and environment variables with their respective values.
  • ADD copies new files, directories or remote file URLs from the source and adds them to the filesystem of the image at the provided path. In our case we add the Spring Boot application to the Docker image from the source path (the JAR_FILE variable) to a destination named app.jar.
  • ENTRYPOINT specifies the command that Docker will use to run our app. In this case it will pass the common command to run a jar — java -jar <name of the jar> — so in this case it is java -jar app.jar to our ENTRYPOINT option (remember that we renamed the spring-boot-docker.jar file to app.jar).

That’s all for the configuration.

check out another of Erick’s tutorials
Creating_a_streaming_pipeline_in_Apache_Beam_preview.png
java

creating a streaming pipeline in apache beam

4

read morego to

Generate a .jar file

Now we need to generate our .jar file running the mvn install command in your IntelliJ terminal. This will generate a .jar file with all the classes from our application.

When the process is finished, go to the target folder from your project and see the spring-boot-docker.jar file there.


Build a Spring Boot Docker image

Great, we’re done with the preparations, and it’s time to build our image using Docker Engine.

Make sure you have Docker Engine installed. To check it, run the docker ps or docker info command on the terminal screen. If the command is not found, you may need to install Docker first. In this case please follow this link and find the installer for your OS.

Run your Docker Engine. Find the folder with the Dockerfile of your Spring Boot project in the terminal and execute the following command (make sure to end the command with a space and a dot):

docker build -t spring-boot-docker:spring-docker .

The “build” command will build an image according to the instructions that we passed to the Dockerfile and the -t flag is used to add a tag for our image.

In a few minutes you will see that the image was successfully created:

A screenshot of the status message shown when a Spring Boot Docker image is created

Run the docker image ls command to check if the image exists. You should be able to see your image in the repository.

A screenshot of checking a Spring Boot Docker image in the repository
learn to solve Java cold start problems
28688_—_GraalVM_AWS_Lambda_or_solving_Java_cold_start_problem_(2).png
java

using GraalVM & AWS Lambda in Java for cold start problems

5

read morego to

Run the Spring Boot Docker image in a container

Now we will run our image in a container, but first we will make sure we won’t get an error trying to point our container port with the localhost one.

Run the next command:

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

You may add the -d flag before -p to avoid seeing any logs and run the container in the background mode.

The flag -p creates a firewall rule which maps the previously exposed container port :8080 to the :8080 port on your machine.

If everything is done right, our container should be running.

A screenshot showing if a Spring Boot container is running

Then run the curl -v localhost:8080/api/hello command in your terminal to test if we can access the endpoint of the application.

You must see the same response as in the beginning of the tutorial.

A screenshot showing a Spring Boot application response during dockerizaion

That’s it! I hope you learned how to dockerize a Spring Boot application.

If you’re interested in joining EPAM Anywhere, feel free to browse our remote Java developer jobs and apply.

Happy coding!

we'll be happy to share our latest stories with yousubscribe to get the latest tech insights, career growth, and lifestyle tips right in your inbox

By subscribing, you agree to receive marketing emails from EPAM Anywhere and other partners offers and accept our Privacy Policy. You can opt-out at any time.