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<br/>WORKDIR extracted<br/>ADD ./target/*.jar app.jar<br/>RUN java -Djarmode=layertools -jar app.jar extract<br/><br/>FROM eclipse-temurin:17.0.5_8-jre-focal<br/>WORKDIR application<br/>COPY --from=builder extracted/dependencies/ ./<br/>COPY --from=builder extracted/spring-boot-looder/ ./<br/>COPY --from=builder extracted/snapshot-dependencies/ ./<br/>COPY --from=builder extracted/application/ ./<br/>EXPOSE 8085<br/>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<br/>ENV SPRING_PROFILES_ACTIVE=production<br/>COPY target/my-application.jar app.jar<br/>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<br/>COPY target/my-application.jar app.jar<br/>HEALTHCHECK --interval=5s \
--timeout=3s \
CMD curl -f http://localhost:8080/actuator/health || exit 1<br/>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<br/>WORKDIR /app<br/>COPY pom.xml .<br/>RUN mvn dependency:go-offline<br/>COPY src/ ./src/<br/>RUN mvn package -DskipTests<br/><br/>FROM openjdk:11<br/>COPY --from=builder /app/target/my-application.jar app.jar<br/>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: *<br/>!src/<br/>!pom.xml<br/>target/ Adding metadata labels to the image improves maintainability. Example:
FROM openjdk:11<br/>LABEL maintainer="John Doe <[email protected]>"<br/>LABEL version="1.0"<br/>LABEL description="My Spring Boot application"<br/>COPY target/my-application.jar app.jar<br/>ENTRYPOINT ["java","-jar","/app.jar"]These practices together produce lean, secure, and easily manageable Docker images for Spring Boot applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
