Dockerize a Spring Boot Application
Starting with this tutorial, I will go through a deployment series of a Java application to various cloud providers.
Before setting up a pipeline and walking you through the deployment process, I want to dockerize an application so we can use this in the deployment process.
What is Docker, and why do we need to Dockerize an application?
Docker is a Linux-based container management tool that enables you to create, publish and consume container images.
Containerization of an application with Docker will create a consistent environment in recreating applications and deploying them to the different cloud providers. That commits write once, run anywhere.
- Containerized apps start and stop very quickly.
- Docker helps efficiency when it comes to the usage of system resources
- Ability to run on different platforms & cloud providers
- Isolation between host system and application
- Easy to scale
I’m writing this tutorial with the prerequisites below;
- You have Docker installed, up and running!
Dockerize a Spring Boot Application
Initialize a Spring Boot Project
I have already created an elementary Spring Boot application for this tutorial. It provides a simple GET endpoint with /ping path and returns “pong” as a response. You can clone from Exceptionly Github account or generate a Spring Boot application with Spring Initializr
After we clone the application, build an executable jar file by Gradle build task on the left Gradle UI or with ./gradlew build command from the terminal.
Containerize Application With Docker
Docker is building images automatically by executing the instructions from a Dockerfile.
Official Docker Docs statement for Dockerfile:
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.
Create the following Dockerfile in the application root folder
FROM eclipse-temurin:17 COPY build/libs/*.jar app.jar ENTRYPOINT ["java","-jar","/app.jar"]
Eclipse Temurin is providing the official OpenJDK binary images. In the above example, we are using Java 17 version.
Find the Eclipse Temurin images in the Docker hub.
This docker build does the following:
- It starts from the Eclipse Temurin official OpenJDK image for Java 17
- Copies the Java executable jar inside the container
- Configures the container for the instructions in the ENTRYPOINT
Run this Dockerfile with the command to build an image;
$ docker build -t exceptionly/demo .
This command will build an image named exceptionly/demo from the Dockerfile.
Verify built image;
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE exceptionly/demo latest bd276cd342d0 15 minutes ago 480MB
Run the dockerized application container with the following command:
-p binds port 8080 of the container to TCP port 8080 on localhost
$ docker run -p 8080:8080 exceptionly/demo :: Spring Boot :: (v2.6.0) Starting DemoApplication using Java 17.0.1 on 755fff7c521e with PID 1 (/app.jar started by root in /) … Started DemoApplication in 4.249 seconds (JVM running for 4.95)
List the containers that are running currently:
$ docker ps c13124bf328d exceptionly/demo:latest "java -jar /app.jar" 5 seconds ago Up 4 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
The Spring Boot application is up and running in a Docker container. Navigate localhost:8080/ping endpoint in the browser or curl localhost:8080/ping from the command line.
$ curl http://localhost:8080/ping pong
As always being said; The best way to learn a tool or language is to practice.
Try to dockerize an application that has a different tech stack. Feel free to check out my previous posts to initialize an application if you haven’t yet!
“Exceptionly was such a fast and efficient process. Complete game changer for hiring remote.”
“Great service, outstanding speed, stunning quality.”
Is Exceptionly an outsourcing business?
No. We encourage our clients to do direct hires. We provide payroll consultancy and services in a 100% transparent way if our clients are new to remote hiring.
Is Exceptionly a database of candidates?
Not really, data is only a small part of our business. While we own over 2 million publicly available software engineer data evaluated, our power lies in hands-on testing and engineer-to-engineer technical interviews.
Is Exceptionly expensive?
Exceptionly is more cost-efficient than any other serious competitor who owns a decent technical testing funnel. If you compare Exceptionly with HR agencies who only do resume screening before endorsement, yes, it is expensive.
Who is the founder of Exceptionly?
Sinan Ata is the founder of Exceptionly, who previously built two global technical talent acquisition enginess and tested over 2.5 million software engineers in his career.
Still have questions? Contact us.
Ready to meet top-notch engineers?
Let's get your talent pipelines up and running!
No hidden fees.