In this article, we will see how to containerize the Spring Boot applications with Buildpacks. In one of the previous articles, I discussed Jib. Jib allows us to build any Java application as the docker image without Dockerfile. Now, starting with Spring Boot 2.3, we can directly containerize the Spring Boot application as a Docker image as Buildpacks support is natively added to the Spring Boot. With Buildpacks support any Spring Boot 2.3 and above applications can be containerized without the Dockerfile. I will show you how to do that with a sample Spring Boot application by following the below steps.
Step 1: Make sure that you have installed Docker.
Step 2: Create a Spring Boot application using version Spring Boot 2.3 and above. Below is the Maven configuration of the application.
If you want to use Gradle, here is the Spring Boot Gradle plugin.
Step 3: I have added a simple controller to test the application once we run the docker container of our Spring Boot app. Below is the controller code.
Step 4: Go to the root folder of the application and run the below command to generate the Docker image. Buildpacks uses the artifact id and version from the pom.xml to choose the Docker image name.
Step 5: Let’s run the created Docker container image and test our rest endpoint.
Below is the output of the REST endpoint.
Step 6: Now you can publish the Docker image to Dockerhub by using the below command.
Here are some of the references if you want to deep dive into this topic.
- Cloud Native Buildpacks Platform Specification.
- Spring Boot 2.3.0.RELEASE Maven plugin documentation
- Spring Boot 2.3.0.RELEASE Gradle plugin documentation
That’s it. We have created a Spring Boot application as a Docker image with Maven/Gradle configuration. The source code of this article is available on GitHub. We will connect with another topic. Till then, Happy Learning!!