Sign inSign up

shinyay/spring-boot-docker-template

By shinyay

Updated about 6 years ago

Image
0

524

shinyay/spring-boot-docker-template repository overview

Dockerfile template for Spring Boot

Optimized Dockerfile template for Spring Boot to be reduced size and secured.

Description

Multi-stage Docker build
FROM gradle:6.5.1-jdk11 as java-build
  :
RUN gradle assemble
FROM gcr.io/distroless/java:11
  :
COPY --from=java-build
  :
ENTRYPOINT [ "java", ...]
Clean separation between Dependencies and Application resources

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
UseContainerSupport
  • -XX:InitialRAMPercentage
    • Set initial JVM Heap size as a percentage of the total memory
  • -XX:MinRAMPercentage
    • Set the minimal JVM Heap size as a percentage of the total memory
  • -XX:MaxRAMPercentage
    • Set the maximum JVM Heap size as a percentage of the total memory
ENV _JAVA_OPTIONS "-XX:MaxRAMPercentage=80 -XX:MinRAMPercentage=50"
Default settings

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)
Defaul Max Heap size
$ 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
MaxRAMPed Max Heap size
$ 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" Docker Images

"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", ...]

Demo

SonarQube
$ docker run --rm -d --name sonarqube -p 9000:9000 sonarqube

Log in to http://localhost:9000 with System Administrator credentials (login=admin, password=admin).

sonarqube-token

  • build.gradle.kts
plugins {
	id("org.sonarqube") version "3.0"
}
  • gradle.properties
systemProp.sonar.host.url=http://localhost:9000
systemProp.sonar.login=<TOKEN>
$ ./gradlew sonarqube

Custom JRE

Extracting JAR -> Jdeps -> Jlink

Extracting JAR
$ cd build/libs/ && jar xvf SPRING_BOOT.jar && cd -
Jdeps
$ 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

Features

Docker Experimental Features
Experimental Features Enabled
  • ~/.docker/config.json
    • "experimental": "enabled"
{
        "auths": {
        },
        "experimental": "enabled"
}
$ docker buildx --help

Usage:	docker buildx COMMAND
Dockerfile for Eperimental Features
# syntax=docker/dockerfile:experimental
FROM ...
Mount Gradle Build Cache
  • --mount=type=cache
RUN --mount=type=cache,id=gradle,target=/home/gradle/.gradle gradle assemble --no-daemon
Docker Build with Buildx
$ 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

Requirement

Usage

Installation

Licence

Released under the MIT license

Author

shinyay

Tag summary

Content type

Image

Digest

Size

88.9 MB

Last updated

about 6 years ago

docker pull shinyay/spring-boot-docker-template