'How to reduce docker image size for java apps without alpine?

I have an image build upon a eclipse-temurin:11, which runs fine, but results in >600mb for a simple spring-boot hello world webapp.

Question: is my dockerfile wrong, or how could I reduce image size, without having to switch on an alpine/musslibc?

# syntax=docker/dockerfile:1
FROM maven:3.8.4-eclipse-temurin-11 as build
WORKDIR application

COPY pom.xml .
RUN mvn dependency:go-offline

COPY src src
RUN mvn package

RUN cp /application/target/*.jar application.jar
RUN java -Djarmode=layertools -jar application.jar extract

FROM eclipse-temurin:11
WORKDIR application
COPY --from=build application/dependencies/ ./
COPY --from=build application/spring-boot-loader/ ./
COPY --from=build application/snapshot-dependencies/ ./
COPY --from=build application/application/ ./

ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]

Maybe anybody sees a problem regarding the image layer sizes?

~$ docker history myimage:latest 
IMAGE          CREATED             CREATED BY                                      SIZE      COMMENT
e628830c54bf   About an hour ago   /bin/sh -c #(nop)  ENTRYPOINT ["java" "org.s…   0B        
87d052d04e27   About an hour ago   /bin/sh -c #(nop) COPY dir:5d09192487cc563d9…   13.8kB    
8c34a91b2cfb   About an hour ago   /bin/sh -c #(nop) COPY dir:8b993266a653e9e77…   0B        
d4e71b50b4da   About an hour ago   /bin/sh -c #(nop) COPY dir:8b86bf42f46c065b7…   252kB     
fbc788f64a4a   About an hour ago   /bin/sh -c #(nop) COPY dir:961713675a0647292…   26.4MB    
fa2564232f74   2 hours ago         /bin/sh -c #(nop) WORKDIR /application          0B        
3dbb3240fc1f   4 days ago          /bin/sh -c #(nop)  CMD ["jshell"]               0B        
<missing>      4 days ago          /bin/sh -c echo Verifying install ...     &&…   0B        
<missing>      4 days ago          /bin/sh -c #(nop)  ENV JAVA_HOME=/opt/java/o…   0B        
<missing>      4 days ago          /bin/sh -c set -eux;     ARCH="$(dpkg --prin…   322MB     
<missing>      4 days ago          /bin/sh -c #(nop)  ENV JAVA_VERSION=jdk-11.0…   0B        
<missing>      4 days ago          /bin/sh -c apt-get update     && DEBIAN_FRON…   43.2MB    
<missing>      4 days ago          /bin/sh -c #(nop)  ENV LANG=en_US.UTF-8 LANG…   0B        
<missing>      5 days ago          /bin/sh -c #(nop)  CMD ["bash"]                 0B        
<missing>      5 days ago          /bin/sh -c #(nop) ADD file:1d3b09cf9e041d608…   72.8MB    

The problem seems to be the layer with 322mb, where the full jdk is downloaded into the container:

4 days ago /bin/sh -c set -eux; ARCH="$(dpkg --print-architecture)"; case "${ARCH}" in aarch64|arm64) ESUM='79572f5172c6a040591d34632f98a20ed148702bbce2f57649e8ac01c0d2e3db'; BINARY_URL='https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.14.1%2B1/OpenJDK11U-jdk_aarch64_linux_hotspot_11.0.14.1_1.tar.gz'; ;; armhf|arm) ESUM='f4d53a1753cdde830d7872c6a1279df441f3f9aeb5d5037a568b3a392ebce9c2'; BINARY_URL='https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.14.1%2B1/OpenJDK11U-jdk_arm_linux_hotspot_11.0.14.1_1.tar.gz'; ;; ppc64el|powerpc:common64) ESUM='9750e11721282a9afd18a07743f19c699b2b71ce20d02f3f0a906088b9ae6d9a'; BINARY_URL='https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.14.1%2B1/OpenJDK11U-jdk_ppc64le_linux_hotspot_11.0.14.1_1.tar.gz'; ;; s390x|s390:64-bit) ESUM='79a27a4dc23dff38a5c21e5ba9b7efcf0aa5e14ace1a3b19bec53e255c487521'; BINARY_URL='https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.14.1%2B1/OpenJDK11U-jdk_s390x_linux_hotspot_11.0.14.1_1.tar.gz'; ;; amd64|i386:x86-64) ESUM='43fb84f8063ad9bf6b6d694a67b8f64c8827552b920ec5ce794dfe5602edffe7'; BINARY_URL='https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.14.1%2B1/OpenJDK11U-jdk_x64_linux_hotspot_11.0.14.1_1.tar.gz'; ;; *) echo "Unsupported arch: ${ARCH}"; exit 1; ;; esac; curl -LfsSo /tmp/openjdk.tar.gz ${BINARY_URL}; echo "${ESUM} */tmp/openjdk.tar.gz" | sha256sum -c -; mkdir -p /opt/java/openjdk; cd /opt/java/openjdk; tar -xf /tmp/openjdk.tar.gz --strip-components=1; rm -rf /tmp/openjdk.tar.gz; 322MB

How could I reduce the java layer here?



Solution 1:[1]

One simple solution could be to use the 11-jre image for runtime (eclipse-temurin:11-jre). This would save around 100 MB. Everything else seems to be related to your application (given that the 11 image is around 220 MB in size).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dunni