Dockerizing Your Spring Boot App: A Step-by-Step Guide

By adopting Docker in your Spring Boot development workflow, you've embraced a technology that not only enhances the reliability of your applications but also aligns with modern DevOps practices. As the landscape of software development evolves, Docker remains a valuable tool for achieving consistency, reproducibility, and scalability in deploying your Spring Boot applications. Cheers to your containerized development journey!

In the world of modern application development, containerization has become a key practice for building, deploying, and scaling applications. Docker, with its simplicity and efficiency, has emerged as a popular choice for containerization. In this guide, we will walk through the process of creating a Docker image for your Spring Boot application, allowing you to package your application and its dependencies into a single, portable container.

1. Prerequisites

  1. A Spring Boot application (if not, you can quickly scaffold one using Spring Initializr).
  2. Docker installed on your development machine. You can download Docker from here.

2. Add pull policy and image tag

add pull policy and image tag in your build section in your pom.xml file. In your image.name tag there are 3 things you need to specify

  1. docker user name
  2. name of your application
  3. version of your application

so the final syntax will look like this {docker_user_name}/{application_name}:{application_version}

          <build>
                  <plugins>
                      <plugin>
                          <groupId>org.springframework.boot</groupId>
                          <artifactId>spring-boot-maven-plugin</artifactId>
                          <configuration>
                              <image>
                                  <name>{your_docker_user_name}/demo-${project.artifactId}:${project.version}</name>
                              </image>
                              <pullPolicy>IF_NOT_PRESENT</pullPolicy>
                      </configuration>
                      </plugin>
                  </plugins>
          </build>
          

3. Set a maven goal

            mvn spring-boot:build-image
          

this process will take few minutes, after completion of the process you will see a success or failure message in the console

          [INFO] Successfully built image 'docker.io/{your_docker_user_name}/demo-learn-docker:0.0.1-SNAPSHOT'
          [INFO]
          [INFO] ------------------------------------------------------------------------
          [INFO] BUILD SUCCESS
          [INFO] ------------------------------------------------------------------------
          [INFO] Total time:  02:58 min
          [INFO] Finished at: 2023-12-14T19:51:14+05:30
          [INFO] ------------------------------------------------------------------------
          

if your image is successfully created on docker hug registry then one link prefixed by docker.io will be printed in the log

bingo! our application is successfully converted to a docker image and our image path will be {your_docker_user_name}/demo-learn-docker:0.0.1-SNAPSHOT, this is the image path that we will be using for creating a docker container for our image.

4. Run your application inside Docker Container

            viveksoni@viveksoni100:~$ docker run -p 9190:9090 viveksoni100/demo-learn-docker:0.0.1-SNAPSHOT
          

here -p stands for port, my 9190 is a local port and 9090 is a docker configured port

you'll see your application's log in the terminal and your container will also available in docker desktop

terminal log showing a spring boot application startup


docker desktop showing containers


5. Testing our application

As we deployed our application successfully as a docker container, lets check if it is working fine

go to your favorite browser and hit http://localhost:9190/motivate

you'll see random motivational quotes are reflecting on your browser

motivational quote - 1


motivational quote - 2


congratulations! we've successfully deployed our springboot application to docker registry, and containerize it. From now on you just have to give your image path to the deployment team and they'll up your service within minutes, they don't have to install Java, Tomcat or any other dependencies just run your image in the docker container and magic will unfold by itself

Conclusion

Dockerizing your Spring Boot application is a transformative step that brings numerous benefits to your development and deployment processes. By encapsulating your application and its dependencies into a portable container, you've created an environment-agnostic package that can run consistently across various platforms. The ability to deploy this containerized application seamlessly provides advantages in terms of scalability, resource utilization, and ease of management.

As you continue your journey into containerization, consider exploring advanced Docker features such as multi-container setups with Docker Compose, optimizing Docker images for size and performance, and integrating Docker with orchestration tools like Kubernetes for scalable and resilient deployments.


source code

Get in touch

Let’s work together

Reach out if you have a concept for a website or mobile app or require guidance with product design. contact me.
  info@whywhytechnova.com
  +(91) 88661 28862