Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Options Galore: From Source Code to Container I...

Options Galore: From Source Code to Container Image

A typical workflow in a modern software dev project can look like: Build code, put built artifact into container image, put container image into registry, deploy to Kubernetes. Each step has it’s own requirements and pitfalls alike. The overall goal is most often to bake those steps into easily repeatable pipelines and enable a high degree of automation.

Dockerfiles seems to be the choice with the highest adoption when it comes to containerizing code artifacts. However there are options, which might remove some of the pitfalls and standardize the entire process even more.

The talk will give deeper insights by comparing (multi-stage) Dockerfiles to Cloud-Native Buildpacks (buildpacks.io/paketo.io) and Google’s JIB under the evaluation criteria of build time, build size, standardization, robustness and security. The examples and live demo will have certain focus on Java-based frameworks (Spring Boot, Quarkus, Java EE), but coverage of other languages will also be included and highlighted.

The intented take-away of the session is a better overview of container building and deployment options along with understanding of requirements, advantages and drawbacks.

Matthias Haeussler

April 27, 2021
Tweet

More Decks by Matthias Haeussler

Other Decks in Technology

Transcript

  1. Dockerfiles, Buildpacks, Jib and more ... what's the best way

    to run your Java code in Containers? Matthias Haeussler, Chief Technologist @maeddes
  2. 2

  3. @maeddes | 20 FROM ubuntu:24.04 RUN apt update && apt

    install openjdk-21-jre-headless -y COPY target/simplecode-0.0.1-SNAPSHOT.jar /opt/app.jar CMD ["java","-jar","/opt/app.jar"] Dockerfile (simple)
  4. @maeddes | Be careful - This will still “work”! 26

    FROM adoptopenjdk:11-jre-hotspot COPY target/simplecode-0.0.1-SNAPSHOT.jar /opt/app.jar CMD ["java", "-jar", "/opt/app.jar"]
  5. @maeddes | Multi-Stage Dockerfile 28 FROM maven:3-eclipse-temurin-21 AS build RUN

    mkdir -p /opt/app/src COPY src /opt/app/src COPY pom.xml /opt/app RUN mvn -f /opt/app/pom.xml package FROM eclipse-temurin:21-jre COPY --from=build /opt/app/target/simplecode-0.0.1-SNAPSHOT.jar /opt/app.jar ENTRYPOINT ["java","-jar","/opt/app.jar"]
  6. @maeddes | Multi-Stage Dockerfile 29 FROM maven:3-eclipse-temurin-21 AS build RUN

    mkdir -p /opt/app/src COPY src /opt/app/src COPY pom.xml /opt/app RUN mvn -f /opt/app/pom.xml package FROM eclipse-temurin:21-jre COPY --from=build /opt/app/target/simplecode-0.0.1-SNAPSHOT.jar /opt/app.jar ENTRYPOINT ["java","-jar","/opt/app.jar"]
  7. @maeddes | Multi-Stage Dockerfile 30 FROM maven:3-eclipse-temurin-21 AS build RUN

    mkdir -p /opt/app/src COPY src /opt/app/src COPY pom.xml /opt/app RUN mvn -f /opt/app/pom.xml package FROM eclipse-temurin:21-jre COPY --from=build /opt/app/target/simplecode-0.0.1-SNAPSHOT.jar /opt/app.jar ENTRYPOINT ["java","-jar","/opt/app.jar"]
  8. @maeddes | Mount Cache 34 FROM maven:3-eclipse-temurin-21 AS build RUN

    mkdir -p /opt/app/src COPY src /opt/app/src COPY pom.xml /opt/app RUN --mount=type=cache,target=/root/.m2 mvn -f /opt/app/pom.xml package FROM eclipse-temurin:21-jre COPY --from=build /opt/app/target/simplecode-0.0.1-SNAPSHOT.jar /opt/app.jar ENTRYPOINT ["java","-jar","/opt/app.jar"]
  9. @maeddes | • Speed • Size • Structure • Standardization

    • Simplicity • Security Criteria 36
  10. @maeddes | • Speed • Size • Structure • Standardization

    • Simplicity • Security • JVM configuration Criteria 37
  11. @maeddes | Layered Jar 41 FROM eclipse-temurin:21-jre AS builder COPY

    --from=maven /opt/app/target/simplecode-0.0.1-SNAPSHOT.jar application.jar RUN java -Djarmode=layertools -jar application.jar extract FROM eclipse-temurin:21-jre COPY --from=builder application/dependencies/ ./ COPY --from=builder application/spring-boot-loader/ ./ COPY --from=builder application/snapshot-dependencies/ ./ COPY --from=builder application/application/ ./ ENTRYPOINT ["java","org.springframework.boot.loader.JarLauncher"]
  12. @maeddes | Entire file 42 FROM maven:3-eclipse-temurin-21 AS maven RUN

    mkdir -p /opt/app/src COPY src /opt/app/src COPY pom.xml /opt/app RUN --mount=type=cache,target=/root/.m2 mvn -f /opt/app/pom.xml package FROM eclipse-temurin:21-jre AS builder WORKDIR application COPY --from=maven /opt/app/target/simplecode-0.0.1-SNAPSHOT.jar application.jar RUN java -Djarmode=layertools -jar application.jar extract FROM eclipse-temurin:21-jre WORKDIR application COPY --from=builder application/dependencies/ ./ COPY --from=builder application/spring-boot-loader/ ./ COPY --from=builder application/snapshot-dependencies/ ./ COPY --from=builder application/application/ ./ ENTRYPOINT ["java","-XX:+UseParallelGC","-XX:MaxRAMPercentage=75","org.springframework.boot.loader.JarLauncher"]
  13. 44

  14. @maeddes | jlink and jdeps 45 FROM maven:3-eclipse-temurin-21 AS build

    RUN --mount=type=cache,target=/root/.m2 mvn package RUN jdeps --ignore-missing-deps -q \ --recursive \ --multi-release 21 \ --print-module-deps \ --class-path 'BOOT-INF/lib/*' \ target/simplecode-0.0.1-SNAPSHOT.jar > deps.info RUN jlink \ --add-modules $(cat deps.info) \ --strip-debug \ --compress 2 \ --no-header-files \ --no-man-pages \ --output /myjre FROM debian:bookworm-slim COPY --from=build /myjre $JAVA_HOME COPY --from=build /usr/src/project/target/simplecode-0.0.1-SNAPSHOT.jar /project/
  15. 46 FROM maven:3-eclipse-temurin-21 AS build RUN mkdir /opt/app COPY src

    /opt/app/src COPY pom.xml /opt/app WORKDIR /opt/app RUN --mount=type=cache,target=/root/.m2 mvn package -DskipTests RUN jar xf target/simplecode-0.0.1-SNAPSHOT.jar RUN jdeps --ignore-missing-deps -q \ --recursive \ --multi-release 21 \ --print-module-deps \ --class-path 'BOOT-INF/lib/*' \ target/simplecode-0.0.1-SNAPSHOT.jar > deps.info RUN jlink \ --add-modules $(cat deps.info) \ --strip-debug \ --compress 2 \ --no-header-files \ --no-man-pages \ --output /myjre FROM eclipse-temurin:21-jre AS extractor RUN mkdir /opt/app WORKDIR /opt/app COPY --from=build /opt/app/target/simplecode-0.0.1-SNAPSHOT.jar application.jar RUN java -Djarmode=layertools -jar application.jar extract FROM ubuntu:jammy ENV JAVA_HOME /opt/java/jdk21 ENV PATH $JAVA_HOME/bin:$PATH COPY --from=build /myjre $JAVA_HOME RUN mkdir /opt/app WORKDIR /opt/app COPY --from=extractor /opt/app/dependencies/ ./ COPY --from=extractor /opt/app/spring-boot-loader/ ./ COPY --from=extractor /opt/app/snapshot-dependencies/ ./ COPY --from=extractor /opt/app/application/ ./ ENTRYPOINT ["java","-XX:+UseParallelGC","-XX:MaxRAMPercentage=75","org.springframework.boot.loader.JarLauncher"]
  16. 51

  17. @maeddes | Summary 69 • Be aware of options and

    possibilities • Be aware of the possibilities of your options • Keep your base images and layers as consistent as possible throughout your landscape • Avoid “wild growth” through dockerfiles • Automate and standardize as much as possible • Give jib and buildpacks a try
  18. @maeddes | 70 Dockerfile (simple) Dockerfile (multistage) Dockerfile (jlink/jdeps) Jib

    Buildpacks/ Paketo Speed 🤩 Size 🤩 Structure 🙂 🙂 🤩 🤩 Standardization 🙂 🙂 🙂 🤩 Simplicity 🙂 🤩 🙂 Security 🙂 🤩 🙂
  19. Novatec Consulting GmbH Bertha-Benz-Platz 1 D-70771 Leinfelden-Echterdingen T. +49 711

    22040-700 [email protected] www.novatec-gmbh.de 72 Chief Technologist Matthias Haeussler Mobil: +49 175 222 5949 E-Mail: [email protected] Twitter: @maeddes
  20. Sources @maeddes | ▪ https://www.excalidraw.com ▪ [1] https://docs.docker.com/engine/release-notes/prior-releases/ ▪ [2]

    https://blog.codecentric.de/en/2020/11/buildpacks-spring-boot/ ▪ [3] https://paketo.io/ ▪ https://buildpacks.io/features/ ▪ https://github.com/GoogleContainerTools/jib ▪ https://www.baeldung.com/docker-layers-spring-boot ▪ https://spring.io/blog/2020/01/27/creating-docker-images-with- spring-boot-2-3-0-m1 ▪ https://github.com/openshift/source-to-image 73