Optimized Dockerfile template for Spring Boot to be reduced size and secured.
FROM gradle:6.5.1-jdk11 as java-build
:
RUN gradle assemble
FROM gcr.io/distroless/java:11
:
COPY --from=java-build
:
ENTRYPOINT [ "java", ...]
Most frequently changing resources, usually the class and static resources in the application itself, to be layered after the more slowly changing resources
COPY --from=java-build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=java-build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=java-build ${DEPENDENCY}/BOOT-INF/classes /app
ENV _JAVA_OPTIONS "-XX:MaxRAMPercentage=80 -XX:MinRAMPercentage=50"
JVM will automatically detect the Control Group memory limit with the UseContainerSupport option.
$ docker run -m 1GB openjdk:11.0.7-jre-slim java \
-XX:+PrintFlagsFinal -version \
| grep -E "UseContainerSupport | InitialRAMPercentage | MaxRAMPercentage | MinRAMPercentage"
WARNING: Your kernel does not support swap limit capabilities or the cgroup is not mounted. Memory limited without swap.
double InitialRAMPercentage = 1.562500 {product} {default}
double MaxRAMPercentage = 25.000000 {product} {default}
double MinRAMPercentage = 50.000000 {product} {default}
bool UseContainerSupport = true {product} {default}
openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment 18.9 (build 11.0.7+10)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.7+10, mixed mode)
$ docker run -m 1GB openjdk:11.0.7-jre-slim java -XshowSettings:vm -version
VM settings:
Max. Heap Size (Estimated): 247.50M
Using VM: OpenJDK 64-Bit Server VM
$ docker run -m 1GB openjdk:11.0.7-jre-slim java -XX:MaxRAMPercentage=80 -XX:MinRAMPercentage=50 -XshowSettings:vm -version
VM settings:
Max. Heap Size (Estimated): 792.69M
Using VM: OpenJDK 64-Bit Server VM
"Distroless" images contain only your application and its runtime dependencies. They do not contain package managers, shells or any other programs you would expect to find in a standard Linux distribution.
FROM gcr.io/distroless/java:11
:
ENTRYPOINT [ "java", ...]
$ docker run --rm -d --name sonarqube -p 9000:9000 sonarqube
Log in to http://localhost:9000 with System Administrator credentials (login=admin, password=admin).

plugins {
id("org.sonarqube") version "3.0"
}
systemProp.sonar.host.url=http://localhost:9000
systemProp.sonar.login=<TOKEN>
$ ./gradlew sonarqube
Extracting JAR -> Jdeps -> Jlink
$ cd build/libs/ && jar xvf SPRING_BOOT.jar && cd -
$ cd build/libs/ && \
jdeps --class-path "BOOT-INF/lib/*" \
--multi-release base \
--ignore-missing-deps \
-recursive \
--print-module-deps \
SPRING_BOOT.jar && \
cd -
java.base,java.desktop,java.instrument,java.management.rmi,java.naming,java.prefs,java.scripting,java.security.jgss,java.sql,jdk.httpserver,jdk.unsupported
FROM openjdk:14-alpine AS jre-build
WORKDIR /jlink
ENV PATH $JAVA_HOME/bin:$PATH
RUN jlink --strip-java-debug-attributes --no-header-files --no-man-pages --compress=2 --module-path $JAVA_HOME \
--add-modules java.base,java.desktop,java.instrument,java.management.rmi,java.naming,java.prefs,java.scripting,java.security.jgss,java.sql,jdk.httpserver,jdk.unsupported \
--output jre-min
{
"auths": {
},
"experimental": "enabled"
}
$ docker buildx --help
Usage: docker buildx COMMAND
# syntax=docker/dockerfile:experimental
FROM ...
RUN --mount=type=cache,id=gradle,target=/home/gradle/.gradle gradle assemble --no-daemon
$ docker buildx build .
$ docker buildx --help
Usage: docker buildx COMMAND
Build with BuildKit
Management Commands:
imagetools Commands to work on images in registry
Commands:
bake Build from a file
build Start a build
create Create a new builder instance
inspect Inspect current builder instance
ls List builder instances
rm Remove a builder instance
stop Stop builder instance
use Set the current builder instance
version Show buildx version information
Released under the MIT license
Content type
Image
Digest
Size
88.9 MB
Last updated
about 6 years ago
docker pull shinyay/spring-boot-template:java8