Best Practices for Dockerizing Spring Boot Applications
This guide explains why Docker is essential for Java developers and provides step‑by‑step best practices—including selecting the right base image, using multi‑stage builds, environment variables, health checks, Docker cache, .dockerignore files, and image labels—complete with ready‑to‑use Dockerfile examples for Spring Boot projects.
Docker enables Java developers to package Spring Boot applications into portable containers that run consistently across environments. Choosing the appropriate base image, such as an OpenJDK JRE layer (e.g., eclipse-temurin:17.0.5_8-jre-focal ), reduces image size and improves performance.
Multi‑stage builds are recommended: the first stage compiles the application and extracts layers with java -Djarmode=layertools -jar app.jar extract , while the second stage uses a slim JRE base image to copy only the necessary files and set the entry point, for example:
FROM eclipse-temurin:17.0.5_8-jre-focal as builder
WORKDIR extracted
ADD ./target/*.jar app.jar
RUN java -Djarmode=layertools -jar app.jar extract
FROM eclipse-temurin:17.0.5_8-jre-focal
WORKDIR application
COPY --from=builder extracted/dependencies/ ./
COPY --from=builder extracted/spring-boot-looder/ ./
COPY --from=builder extracted/snapshot-dependencies/ ./
COPY --from=builder extracted/application/ ./
EXPOSE 8085
ENTRYPOINT ["java","org.springframework.boot.loader.launch.JarLauncher"]Environment variables can configure the application at runtime without rebuilding the image. For instance, setting SPRING_PROFILES_ACTIVE=production in the Dockerfile activates the production profile:
FROM openjdk:11
ENV SPRING_PROFILES_ACTIVE=production
COPY target/my-application.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]Health checks ensure the container’s health is monitored. Adding a HEALTHCHECK directive that curls the Spring Boot actuator endpoint allows Docker to report the container’s status:
FROM openjdk:11
COPY target/my-application.jar app.jar
HEALTHCHECK --interval=5s \
--timeout=3s \
CMD curl -f http://localhost:8080/actuator/health || exit 1
ENTRYPOINT ["java","-jar","/app.jar"]Leveraging Docker’s layer cache speeds up rebuilds. A typical cached multi‑stage build looks like:
FROM openjdk:11 as builder
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src/ ./src/
RUN mvn package -DskipTests
FROM openjdk:11
COPY --from=builder /app/target/my-application.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]Using a .dockerignore file excludes unnecessary files from the build context, reducing image size and improving security. A typical file contains:
*
!src/
!pom.xml
target/Adding metadata labels to the image improves maintainability. Example:
FROM openjdk:11
LABEL maintainer="John Doe
"
LABEL version="1.0"
LABEL description="My Spring Boot application"
COPY target/my-application.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]These practices together produce lean, secure, and easily manageable Docker images for Spring Boot applications.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.