Spring Boot is a Java framework for building applications whether you build backend applications or full-stack applications using Java or Kotlin spring boot is a framework to go with.
Another definition: Spring Boot is an open-source Java-based framework used to create a micro Service. It is used to build stand-alone and production-ready spring applications.
Advantages:
- Security
- Logging
- Connecting to DB
- Dependency Injection
- Configuration
- Microservices
- Production-ready
What are Microservices?
Microservice is an architecture that allows developers to develop and deploy services independently. Each service running has its own process and this achieves the lightweight model to support business applications.
Now the most important term that we need to know why spring boot is so useful and easy to use is because of dependency injection, Now What is Dependency injection?
So before we know about dependency injection let’s first define What is loose coupling?
So, loose coupling is when one object doesn’t totally depend on another object it may get replaced. When two classes, modules, or components have low dependencies on each other, it is called loose coupling.
Dependency injection is a fundamental aspect of a spring framework, through which the spring container injects objects into the other objects or “dependencies” Basically the dependency injection containers (spring containers) are responsible to create the objects and then they will inject it inside the class.
Simply put, this allows for loose coupling of components and moves the responsibility of managing components onto the container.
Using: https://www.baeldung.com/spring-boot-docker-images
Let’s start with containerizing our spring boot application (Reporting tool). Since our FLINT reporting tool has 31 microservices and each microservice has its own docker file, the structure of every docker file of the microservices is almost the same. So without any further ado let’s begin.
FROM eclipse-temurin:17.0.1_12-jre-alpine as builder
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract
Spring Boot JAR file has “layers”. When unpacks “jar file” it has internal and external dependencies packaged together. That’s why we have used the multi-stage build for the docker image. So when any line changes in the code only one layer would get affected by it.
ARG instruction defines a variable that can be passed at build time. The JAR_FILE could be passed in as part of the docker command. So here we are using for Maven.
A jarmode is a special system property that you can set when you launch the jar. It allows the bootstrap code to run something entirely different from your application. For example, something that extracts the layers. Here’s you can launch jar with a layertools jar mode.
Now we will step into the next part of the docker file.
FROM eclipse-temurin:17.0.1_12-jre-alpine
COPY --from=builder dependencies/ ./
COPY --from=builder snapshot-dependencies/ ./
COPY --from=builder spring-boot-loader/ ./
COPY --from=builder application/ ./
ENTRYPOINT ["java","-Dspring.datasource.url=jdbc:postgresql://${POSTGRES_HOST}:5432/pools", "org.springframework.boot.loader.JarLauncher"]
Now we will extract and copy each layer.
All being well, we should now have a layered jar with jarmode support.
These are the layers and order that they should be added.
- dependencies
- snapshot-dependencies
- resources
- application
The builder stage extracts the folders that are needed later. Each of the COPY commands relates to the layers that we listed. We can see each layer from the layered jar get added to the Docker image as its own layer.
Top comments (1)
Also to help you to dockerise a Java app (no especially a Spring boot app), you can use JIB.
It's a library from Google to help to build docker image for Java app. It's quite easy to use and powerful
Here are some links, if you want to check it
dev.to/adaendra/jib-a-powerful-doc...
cloud.google.com/java/getting-star...
github.com/GoogleContainerTools/jib