✦ For everyone, free.

Practical knowledge for real and everyday life

Home

12.2.1.5 Spring Boot Container

A focused guide to Spring Boot Container, connecting core concepts with practical Docker and container operations.

Spring Boot containers follow the standard Java multi-stage build pattern with Spring Boot's own specific considerations — its embedded server eliminating any separate application server requirement, and its plugin's support for layered JARs that improve Docker's layer caching for application updates.

A Typical Spring Boot Multi-Stage Dockerfile

The build stage compiles and packages the application using Maven or Gradle, with Spring Boot's plugin producing an executable JAR.

FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn package -DskipTests

FROM eclipse-temurin:21-jre-alpine
COPY --from=build /app/target/myapp.jar /app/myapp.jar
EXPOSE 8080
CMD ["java", "-jar", "/app/myapp.jar"]
Why Spring Boot's Embedded Server Simplifies the Runtime Image

Since Spring Boot embeds its own server (Tomcat, by default) directly within the application, there's no need for a separate application server installation in the runtime image, unlike some other Java web framework deployment patterns.

EXPOSE 8080

The application's embedded server listens directly on this port, with no separate server process or configuration needed.

Using Spring Boot's Layered JAR Support for Better Caching

Spring Boot's build plugin can produce a JAR organized into separate layers, allowing Docker to cache unchanged dependency layers separately from the more frequently changing application code layer.

RUN java -Djarmode=layertools -jar myapp.jar extract
COPY --from=build /app/dependencies/ ./
COPY --from=build /app/spring-boot-loader/ ./
COPY --from=build /app/application/ ./

This layered approach means a code-only change doesn't require re-transferring the (often much larger) dependencies layer, improving build and push efficiency.

Why Spring Boot Containers Matter

Following Spring Boot's specific containerization conventions — embedded server simplicity, layered JAR support — produces an efficient, idiomatic container image well-suited to this widely used Java framework's particular architecture.