diff --git a/README.md b/README.md index a6fdaba..f54ca08 100644 --- a/README.md +++ b/README.md @@ -7,5 +7,6 @@ Runnable companion projects for build, release and DevOps posts on [ankurm.com]( | [maven4-build-vs-consumer-pom](maven4-build-vs-consumer-pom/) | Maven 4 for Java Developers: Build vs Consumer POM, New Lifecycle and Upgrade Checklist | Maven 4.0.0-rc-7 build POM vs consumer POM, model 4.1.0, `before:`/`after:` phases, `mvnup`, mvnd | | [vuln-scan](vuln-scan/) | Dependency Vulnerability Scanning for Java: OWASP Dependency-Check vs Trivy vs OSV-Scanner | The same project scanned by Dependency-Check 13.0.0, Trivy 0.76.2 and OSV-Scanner 2.7.1: false positives, suppression with expiry, CI gating, SBOM-based scanning | | [jbang](jbang/) | JBang: Run Java Like a Script in 2026 (Dependencies, Templates, Spring Boot and Native Images) | JBang 0.132.1 on JDK 25: `//DEPS`, compact source files, `//SOURCES`, a single-file Spring Boot 4.2 app, templates and aliases, remote scripts and `jbang trust`, `--native` with GraalVM | +| [jib](jib/) | Jib for Spring Boot: Container Images Without a Dockerfile (vs Buildpacks) | Spring Boot 4.2.0 built into an image with Jib 3.5.2 and with Buildpacks: layers, reproducible digests, registry push, failure modes, memory behaviour | MIT licensed. diff --git a/jib/.gitignore b/jib/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/jib/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/jib/README.md b/jib/README.md new file mode 100644 index 0000000..d047f21 --- /dev/null +++ b/jib/README.md @@ -0,0 +1,67 @@ +# jib: container images for Spring Boot without a Dockerfile + +Companion module for the ankurm.com post **Jib for Spring Boot: Container Images Without a Dockerfile (vs Buildpacks)**. +Everything in the post, including every transcript, comes from a file in this module. There is no `docs/` folder on purpose: +the explanation lives in the post. + +| Component | Version | +|---|---| +| Spring Boot | 4.2.0 (GA 2026-11-19) | +| jib-maven-plugin | 3.5.2 (Maven Central 2026-07-14) | +| JDK | Temurin 25.0.5.1+2 (LTS) | +| Buildpacks builder | `paketobuildpacks/ubuntu-resolute-builder` (Spring Boot 4.2 default), Liberica JRE 25.0.5 | +| Docker Engine | 29.9.3 | + +## Quickstart + +```bash +export JAVA_HOME=/path/to/jdk-25 +mvn compile jib:dockerBuild # image jib-demo:1.0.0 in the local Docker daemon +docker run -p 8080:8080 jib-demo:1.0.0 +curl localhost:8080/hello +mvn spring-boot:build-image # the Buildpacks version of the same app: jib-demo:buildpacks +``` + +`mvn jib:build` (no `compile`) fails by design: Jib reads `target/classes` and does not compile for you +(`scripts/08-failures.sh`, scenario 1). + +## Files + +| Path | What it is | +|---|---| +| `pom.xml` | Boot 4.2.0 parent, Jib plugin configuration, Buildpacks configuration, `corp-proxy` profile | +| `src/main/java/.../HelloController.java` | One endpoint that reports the JVM it runs on and its heap | +| `src/main/jib/etc/motd.txt` | Copied to `/etc/motd.txt` in the image by Jib's `extraDirectories` | +| `scripts/lib.sh` | Shared setup. `JIB_BASE` selects the base image source (see the comment inside) | +| `scripts/01..10-*.sh` | One script per scenario; each writes `output/NN-*.txt` | +| `scripts/run-all.sh` | Regenerates all of `output/` (about four minutes) | +| `output/*.txt` | Captured real output, the evidence the post quotes | + +## Output index + +| File | Shows | +|---|---| +| `01-versions.txt` | Toolchain and pinned versions | +| `02-jib-build.txt` | `jib:dockerBuild`, image config, the five layers Jib adds, contents of `/app` | +| `03-run.txt` | The Jib image running and answering | +| `04-layer-cache.txt` | One-line code change: which layer digests change | +| `05-reproducible.txt` | Two builds, identical digest; with `USE_CURRENT_TIMESTAMP`, different digests | +| `06-buildpacks.txt` | `spring-boot:build-image` cold build | +| `07-compare.txt` | Size, layers, build time, shell, memory behaviour at 512 MB and 1 GB | +| `08-failures.txt` | Missing `compile`, JRE older than bytecode, heap defaults, `packaged` mode | +| `09-registry-push.txt` | `jib:build` to a local registry, base image from a registry | +| `10-default-base.txt` | Which base image Jib picks when you do not say | + +## Reproducing in a restricted network + +Two things in the capture environment differ from a normal laptop, and the scripts accommodate both: + +* Docker Hub answered `429 Too Many Requests` for anonymous pulls, so `scripts/lib.sh` defaults the Jib base to + `docker://eclipse-temurin:25-jre` (the copy in the local daemon). `JIB_BASE="" ./scripts/run-all.sh` lets Jib pull from the registry. + `09-registry-push.sh` uses `public.ecr.aws/docker/library/eclipse-temurin:25-jre`, so it is daemon-free on both ends. +* Buildpacks download the JRE from github.com inside the builder container, behind a TLS-intercepting proxy. The `corp-proxy` profile + passes the proxy address and CA certificate to the builder (`PROXY_URL`, `CA_DIR`). + +Timings in `07-compare.txt` come from a two-core shared sandbox; they vary by 2x between runs and are only indicative. + +MIT licensed (see the repository root). diff --git a/jib/output/01-versions.txt b/jib/output/01-versions.txt new file mode 100644 index 0000000..ebd9ec2 --- /dev/null +++ b/jib/output/01-versions.txt @@ -0,0 +1,17 @@ + +=== Toolchain === +openjdk version "25.0.5.1" 2026-11-17 LTS +OpenJDK Runtime Environment Temurin-25.0.5.1+2 (build 25.0.5.1+2-LTS) +OpenJDK 64-Bit Server VM Temurin-25.0.5.1+2 (build 25.0.5.1+2-LTS, mixed mode, sharing) +Apache Maven 3.9.11 (3e54c93a704957b63ee3494413a2b544fd3d825b) +Java version: 25.0.5.1, vendor: Eclipse Adoptium, runtime: /tmp/tools/jdk-25.0.5.1+2 +Docker engine 29.9.3 + +=== Versions pinned by the module === +4.2.0 +25 +3.5.2 + +=== Base image used for Jib (digest of the local copy) === +JIB_BASE=docker://eclipse-temurin:25-jre +id=sha256:4ed132f6832e9804b123e89a849a7b4502756ae64a4ee08ebab939d9d5d0a9fa diff --git a/jib/output/02-jib-build.txt b/jib/output/02-jib-build.txt new file mode 100644 index 0000000..8a0222d --- /dev/null +++ b/jib/output/02-jib-build.txt @@ -0,0 +1,56 @@ + +=== mvn jib:dockerBuild === +$ mvn jib:dockerBuild -Djib.from.image=docker://eclipse-temurin:25-jre +[INFO] Containerizing application to Docker daemon as jib-demo, jib-demo:1.0.0... +[INFO] Building dependencies layer... +[INFO] Building resources layer... +[INFO] Building classes layer... +[INFO] Building extra files layer... +[INFO] Building jvm arg files layer... +[INFO] Container entrypoint set to [java, -XX:MaxRAMPercentage=75, -cp, @/app/jib-classpath-file, com.ankurm.jibdemo.JibDemoApplication] +[INFO] Built image to Docker daemon as jib-demo, jib-demo:1.0.0 +[INFO] BUILD SUCCESS +[INFO] Total time: 6.894 s + +=== Image config === +tags=[jib-demo:1.0.0 jib-demo:latest localhost:5000/jib-demo:1.0.0] +user=1000 +created=1970-01-01T00:00:00Z +exposed=map[8080/tcp:{}] +entrypoint=["java","-XX:MaxRAMPercentage=75","-cp","@/app/jib-classpath-file","com.ankurm.jibdemo.JibDemoApplication"] +layers=11 +size=540348618 bytes + +=== Layers Jib added on top of the base image === +12.3kB jib-maven-plugin:3.5.2 +16.4kB jib-maven-plugin:3.5.2 +32.8kB jib-maven-plugin:3.5.2 +28.7kB jib-maven-plugin:3.5.2 +22.2MB jib-maven-plugin:3.5.2 + +=== What is inside /app (no fat jar) === +classes +jib-classpath-file +jib-main-class-file +libs +resources + +HdrHistogram-2.2.2.jar +commons-logging-1.4.0.jar +jackson-annotations-2.21.jar +jackson-core-3.1.8.jar +jackson-databind-3.1.8.jar +... 49 jars in /app/libs + +/app/classes: +com + +/app/resources: +application.properties +com + +jib-demo image: this file came from src/main/jib/etc/motd.txt via extraDirectories + +/app/resources +/app/classes +/app/libs/spring-boot-starter-webmvc-4.2.0.jar diff --git a/jib/output/03-run.txt b/jib/output/03-run.txt new file mode 100644 index 0000000..203f082 --- /dev/null +++ b/jib/output/03-run.txt @@ -0,0 +1,11 @@ + +=== docker run jib-demo:1.0.0 (memory limit 512m) === +$ curl localhost:18080/hello +{"user":"ubuntu","maxHeapMb":371,"javaVersion":"25.0.5.1+2-LTS","greeting":"hello from a Jib image"} +$ curl localhost:18080/actuator/health +{"groups":["liveness","readiness"],"status":"UP"} + +=== First lines of the container log === +INFO 1 --- [jib-demo] [ main] com.ankurm.jibdemo.JibDemoApplication : Starting JibDemoApplication using Java 25.0.5.1 with PID 1 (/app/classes started by ubuntu in /) +INFO 1 --- [jib-demo] [ main] o.s.boot.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/' +INFO 1 --- [jib-demo] [ main] com.ankurm.jibdemo.JibDemoApplication : Started JibDemoApplication in 2.919 seconds (process running for 3.306) diff --git a/jib/output/04-layer-cache.txt b/jib/output/04-layer-cache.txt new file mode 100644 index 0000000..201561b --- /dev/null +++ b/jib/output/04-layer-cache.txt @@ -0,0 +1,22 @@ + +=== Build 1: unchanged source === +a73f075296f4 19883908 bytes 49 files app/libs/HdrHistogram-2.2.2.jar +c40a9f3e76dd 466 bytes 1 files app/resources/application.properties +a44f4b821f44 1445 bytes 2 files app/classes/com/ankurm/jibdemo/HelloController.class +9a14dd547ae2 760 bytes 2 files app/jib-classpath-file +06bc0bf9c586 294 bytes 1 files etc/motd.txt + +=== Edit one string in HelloController.java, then build 2 === +a73f075296f4 19883908 bytes 49 files app/libs/HdrHistogram-2.2.2.jar +c40a9f3e76dd 466 bytes 1 files app/resources/application.properties +1059a437a912 1437 bytes 2 files app/classes/com/ankurm/jibdemo/HelloController.class +9a14dd547ae2 760 bytes 2 files app/jib-classpath-file +06bc0bf9c586 294 bytes 1 files etc/motd.txt + +=== Layers whose digest changed (< build 1, > build 2) === +< a44f4b821f44 +> 1059a437a912 + +=== Files in the layer that changed === +app/classes/com/ankurm/jibdemo/HelloController.class +app/classes/com/ankurm/jibdemo/JibDemoApplication.class diff --git a/jib/output/05-reproducible.txt b/jib/output/05-reproducible.txt new file mode 100644 index 0000000..efe3a33 --- /dev/null +++ b/jib/output/05-reproducible.txt @@ -0,0 +1,10 @@ + +=== jib:buildTar twice, compare the image digest and id === +build 1 digest=sha256:01832f5bfbd58a57428f3eeb51c374bcbd672413bdae004ca572497821897a10 id=sha256:35ae49d238eec2ce1035a26a4ac756a0b907e9078902ad28f2bda385ec9ca8d6 +build 1 tar sha256 prefix=271ade52b5964451652d +build 2 digest=sha256:01832f5bfbd58a57428f3eeb51c374bcbd672413bdae004ca572497821897a10 id=sha256:35ae49d238eec2ce1035a26a4ac756a0b907e9078902ad28f2bda385ec9ca8d6 +build 2 tar sha256 prefix=271ade52b5964451652d + +=== Same build with creationTime=USE_CURRENT_TIMESTAMP === +build 1 digest=sha256:4345a4fbc0cfb58ebf670afcddeedb2f7d3d3f513d9870e97ed1123390f58813 +build 2 digest=sha256:093f5af3e98f69abc751cb7bd500da6fec95ac55f990e6b38766074ff904894d diff --git a/jib/output/06-buildpacks.txt b/jib/output/06-buildpacks.txt new file mode 100644 index 0000000..bafb122 --- /dev/null +++ b/jib/output/06-buildpacks.txt @@ -0,0 +1,27 @@ + +=== mvn spring-boot:build-image (cold build: cleanCache=true; key lines of the build log) === +[INFO] Building image 'docker.io/library/jib-demo:buildpacks' + ===> BUILDING + Paketo Buildpack for CA Certificates 3.14.8 + Paketo Buildpack for BellSoft Liberica 11.10.6 + Using Java version 25 extracted from MANIFEST.MF + BellSoft Liberica JRE 25.0.5: Contributing to layer + Downloading from https://github.com/bell-sw/Liberica/releases/download/25.0.5.1+5/bellsoft-jre25.0.5.1+5-linux-amd64.tar.gz + Paketo Buildpack for Syft 2.44.2 + Downloading from https://github.com/anchore/syft/releases/download/v1.57.1/syft_1.57.1_linux_amd64.tar.gz + Paketo Buildpack for Executable JAR 6.17.9 + Paketo Buildpack for Spring Boot 5.41.1 +[INFO] Successfully built image 'docker.io/library/jib-demo:buildpacks' +[INFO] BUILD SUCCESS +[INFO] Total time: 18.197 s + +=== Image config === +tags=[jib-demo:buildpacks] +user=1002:1001 +created=1980-01-01T00:00:01Z +entrypoint=["/cnb/process/web"] +layers=18 +size=658601199 bytes + +=== Wall time of this build (seconds, indicative) === +20 diff --git a/jib/output/07-compare.txt b/jib/output/07-compare.txt new file mode 100644 index 0000000..a930c01 --- /dev/null +++ b/jib/output/07-compare.txt @@ -0,0 +1,36 @@ + +=== Cold builds (Jib: local Jib cache removed; Buildpacks: cleanCache=true), seconds === +jib cold: 9 +buildpacks cold: 19 + +=== Warm rebuilds after a one-line change, seconds === +jib warm: 10 +buildpacks warm: 13 + +=== Images (docker image inspect) === +jib-demo:1.0.0 size=540348618 layers=11 created=1970-01-01T00:00:00Z user=1000 +jib-demo:buildpacks size=658601199 layers=18 created=1980-01-01T00:00:01Z user=1002:1001 + +=== Is there a shell in the final image? (docker run --entrypoint sh ... -c 'echo yes') === +jib-demo:1.0.0 yes +jib-demo:buildpacks docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: exec: "sh": executable file not found in $PATH + +=== Run each image under a memory limit === +--- jib-demo:1.0.0 memory=512m +{"maxHeapMb":371,"user":"ubuntu","greeting":"hello from a Jib image","javaVersion":"25.0.5.1+2-LTS"} +container memory in use: 141.6MiB / 512MiB +INFO 1 --- [jib-demo] [ main] com.ankurm.jibdemo.JibDemoApplication : Started JibDemoApplication in 2.558 seconds (process running for 2.85) +--- jib-demo:buildpacks memory=512m +container exited with code 82 +unable to calculate memory configuration +fixed memory regions require 591743K which is greater than 512M available for allocation: -XX:MaxDirectMemorySize=10M, -XX:MaxMetaspaceSize=79743K, -XX:ReservedCodeCacheSize=240M, -Xss1M * 250 threads +--- jib-demo:buildpacks memory=512m -e BPL_JVM_THREAD_COUNT=50 +{"greeting":"hello from a Jib image","user":"cnb","maxHeapMb":131,"javaVersion":"25.0.5.1+5-LTS"} +container memory in use: 136.9MiB / 512MiB +Calculated JVM Memory Configuration: -XX:MaxDirectMemorySize=10M -Xmx137344K -XX:MaxMetaspaceSize=79743K -XX:ReservedCodeCacheSize=240M -Xss1M (Total Memory: 512M, Thread Count: 50, Loaded Class Count: 11665, Headroom: 0%) +INFO 1 --- [jib-demo] [ main] com.ankurm.jibdemo.JibDemoApplication : Started JibDemoApplication in 3.036 seconds (process running for 3.359) +--- jib-demo:buildpacks memory=1g +{"javaVersion":"25.0.5.1+5-LTS","maxHeapMb":433,"user":"cnb","greeting":"hello from a Jib image"} +container memory in use: 132.4MiB / 1GiB +Calculated JVM Memory Configuration: -XX:MaxDirectMemorySize=10M -Xmx456832K -XX:MaxMetaspaceSize=79743K -XX:ReservedCodeCacheSize=240M -Xss1M (Total Memory: 1G, Thread Count: 250, Loaded Class Count: 11665, Headroom: 0%) +INFO 1 --- [jib-demo] [ main] com.ankurm.jibdemo.JibDemoApplication : Started JibDemoApplication in 2.895 seconds (process running for 3.259) diff --git a/jib/output/08-failures.txt b/jib/output/08-failures.txt new file mode 100644 index 0000000..c6483d7 --- /dev/null +++ b/jib/output/08-failures.txt @@ -0,0 +1,22 @@ + +=== 1. jib:buildTar without compiling first (target/ was just cleaned) === +$ mvn clean jib:buildTar +[ERROR] Failed to execute goal com.google.cloud.tools:jib-maven-plugin:3.5.2:buildTar (default-cli) on project jib-demo: Obtaining project build output files failed; make sure you have compiled your project before trying to build the image. (Did you accidentally run "mvn clean jib:build" instead of "mvn clean compile jib:build"?): /target/classes -> [Help 1] + +=== 2. Base image older than the compiled bytecode (class files are Java 25, JRE is 21) === +$ docker run jib-demo-wrongjre +Error: LinkageError occurred while loading main class com.ankurm.jibdemo.JibDemoApplication + java.lang.UnsupportedClassVersionError: com/ankurm/jibdemo/JibDemoApplication has been compiled by a more recent version of the Java Runtime (class file version 69.0), this version of the Java Runtime only recognizes class file versions up to 65.0 + +=== 3. Heap size at a 512 MB container limit: JVM default vs -XX:MaxRAMPercentage=75 === +MaxRAMPercentage=25 + size_t MaxHeapSize = 134217728 {product} {ergonomic} +MaxRAMPercentage=75 + size_t MaxHeapSize = 402653184 {product} {ergonomic} + +=== 4. containerizingMode=packaged after mvn package (Spring Boot repackaged the jar) === +22327669 target/jib-demo-1.0.0.jar +4562 target/jib-demo-1.0.0.jar.original +$ docker run --rm --entrypoint ls -l /app/classpath +4562 jib-demo-1.0.0.original.jar +INFO 1 --- [jib-demo] [ main] com.ankurm.jibdemo.JibDemoApplication : Started JibDemoApplication in 2.697 seconds (process running for 3.013) diff --git a/jib/output/09-registry-push.txt b/jib/output/09-registry-push.txt new file mode 100644 index 0000000..6928ac2 --- /dev/null +++ b/jib/output/09-registry-push.txt @@ -0,0 +1,26 @@ + +=== mvn jib:build to localhost:5000 (base image pulled from public.ecr.aws/docker/library/eclipse-temurin:25-jre) === +$ mvn compile jib:build -Dimage.name=localhost:5000/jib-demo -Djib.allowInsecureRegistries=true +[INFO] Containerizing application to localhost:5000/jib-demo, localhost:5000/jib-demo:1.0.0... +[INFO] Getting manifest for base image public.ecr.aws/docker/library/eclipse-temurin:25-jre... +[INFO] Building dependencies layer... +[INFO] Building classes layer... +[INFO] Building extra files layer... +[INFO] Building jvm arg files layer... +[INFO] Building resources layer... +[INFO] Using base image with digest: sha256:4ed132f6832e... +[INFO] Built and pushed image as localhost:5000/jib-demo, localhost:5000/jib-demo:1.0.0 +[INFO] BUILD SUCCESS + +=== What the registry now holds === +$ curl localhost:5000/v2/_catalog +{"repositories":["jib-demo"]} + +$ curl localhost:5000/v2/jib-demo/tags/list +{"name":"jib-demo","tags":["latest","1.0.0"]} + +$ manifest media type and layer count +application/vnd.docker.distribution.manifest.v2+json layers: 11 + +=== Pull and run what Jib pushed === +jib-demo image: this file came from src/main/jib/etc/motd.txt via extraDirectories diff --git a/jib/output/10-default-base.txt b/jib/output/10-default-base.txt new file mode 100644 index 0000000..3240a78 --- /dev/null +++ b/jib/output/10-default-base.txt @@ -0,0 +1,8 @@ + +=== mvn compile jib:buildTar with no base-image override (java.version=25 in the pom) === +$ mvn compile jib:buildTar +[INFO] Getting manifest for base image eclipse-temurin:25-jre... +[INFO] BUILD FAILURE +[ERROR] Failed to execute goal com.google.cloud.tools:jib-maven-plugin:3.5.2:buildTar (default-cli) on project jib-demo: 429 Too Many Requests +[ERROR] GET https://registry-1.docker.io/v2/library/eclipse-temurin/manifests/25-jre +[ERROR] {"errors":[{"code":"TOOMANYREQUESTS","message":"You have reached your unauthenticated pull rate limit. Sign in for a higher limit: https://www.docker.com/increase-rate-limit"}]} diff --git a/jib/pom.xml b/jib/pom.xml new file mode 100644 index 0000000..909ecac --- /dev/null +++ b/jib/pom.xml @@ -0,0 +1,111 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 4.2.0 + + + + com.ankurm.jibdemo + jib-demo + 1.0.0 + jib-demo: Spring Boot image without a Dockerfile + + + 25 + 3.5.2 + + jib-demo + + + + + org.springframework.boot + spring-boot-starter-webmvc + + + org.springframework.boot + spring-boot-starter-actuator + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + ${image.name}:buildpacks + + + + + + com.google.cloud.tools + jib-maven-plugin + ${jib.version} + + + ${image.name} + + ${project.version} + + + + + 8080 + + + -XX:MaxRAMPercentage=75 + + 1000 + + jib-demo + https://ankurm.com/git.app/asmhatre/java-devops-demo + + + + + + + + + + + + corp-proxy + + + + org.springframework.boot + spring-boot-maven-plugin + + + host + + ${proxy.url} + ${proxy.url} + + + ${ca.dir}:/platform/bindings/ca-certs:ro + + + + + + + + + diff --git a/jib/scripts/01-versions.sh b/jib/scripts/01-versions.sh new file mode 100755 index 0000000..919b067 --- /dev/null +++ b/jib/scripts/01-versions.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +source "$(dirname "$0")/lib.sh" +{ + banner "Toolchain" + java -version 2>&1 | grep -v '^Picked' + "${MVN[@]}" -version 2>&1 | grep -E '^Apache Maven|^Java version' || true + docker version --format 'Docker engine {{.Server.Version}}' + banner "Versions pinned by the module" + grep -E '4\.2\.0||' pom.xml | sed 's/^ *//' + banner "Base image used for Jib (digest of the local copy)" + echo "JIB_BASE=${JIB_BASE:-}" + docker image inspect eclipse-temurin:25-jre --format 'id={{.Id}}' +} 2>&1 | clean | tee "$OUT/01-versions.txt" diff --git a/jib/scripts/02-jib-build.sh b/jib/scripts/02-jib-build.sh new file mode 100755 index 0000000..89635ea --- /dev/null +++ b/jib/scripts/02-jib-build.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +# jib:dockerBuild, then look inside the result. +source "$(dirname "$0")/lib.sh" +{ + banner "mvn jib:dockerBuild" + echo '$ mvn jib:dockerBuild'"${JIB_BASE:+ -Djib.from.image=$JIB_BASE}" + "${MVN[@]}" jib:dockerBuild "${BASE_ARG[@]}" 2>&1 | sed -n '/--- jib:/,$p' | grep -v 'Finished at' | grep -E 'Containerizing|Building|entrypoint|Built image|BUILD|Total time' + banner "Image config" + docker image inspect jib-demo:1.0.0 --format 'tags={{.RepoTags}} +user={{.Config.User}} +created={{.Created}} +exposed={{.Config.ExposedPorts}} +entrypoint={{json .Config.Entrypoint}} +layers={{len .RootFS.Layers}} +size={{.Size}} bytes' + banner "Layers Jib added on top of the base image" + docker history jib-demo:1.0.0 --no-trunc --format '{{.Size}} {{.CreatedBy}}' | grep jib-maven-plugin + banner "What is inside /app (no fat jar)" + docker run --rm --entrypoint sh jib-demo:1.0.0 -c 'ls -1 /app; echo; ls /app/libs | head -5; echo "... $(ls /app/libs | wc -l) jars in /app/libs"; echo; ls /app/classes /app/resources; echo; cat /etc/motd.txt; echo; cat /app/jib-classpath-file | tr ":" "\n" | head -3' +} 2>&1 | clean | tee "$OUT/02-jib-build.txt" diff --git a/jib/scripts/03-run.sh b/jib/scripts/03-run.sh new file mode 100755 index 0000000..c052dbe --- /dev/null +++ b/jib/scripts/03-run.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +# Run the Jib image and call it. +source "$(dirname "$0")/lib.sh" +docker rm -f jib-demo-run >/dev/null 2>&1 || true +{ + banner "docker run jib-demo:1.0.0 (memory limit 512m)" + docker run -d --name jib-demo-run -m 512m -p 18080:8080 jib-demo:1.0.0 >/dev/null + for i in $(seq 1 60); do curl -s -o /dev/null localhost:18080/actuator/health && break; sleep 1; done + echo '$ curl localhost:18080/hello' + curl -s localhost:18080/hello; echo + echo '$ curl localhost:18080/actuator/health' + curl -s localhost:18080/actuator/health; echo + banner "First lines of the container log" + docker logs jib-demo-run 2>&1 | grep -E 'Starting JibDemoApplication|Tomcat started|Started JibDemoApplication' | sed -E 's/^[0-9T:.Z-]+ +//' +} 2>&1 | clean | tee "$OUT/03-run.txt" +docker rm -f jib-demo-run >/dev/null 2>&1 || true diff --git a/jib/scripts/04-layer-cache.sh b/jib/scripts/04-layer-cache.sh new file mode 100755 index 0000000..3569250 --- /dev/null +++ b/jib/scripts/04-layer-cache.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +# Change one line of application code and see which layers of the image change. +source "$(dirname "$0")/lib.sh" +SRC=src/main/java/com/ankurm/jibdemo/HelloController.java +cp "$SRC" /tmp/HelloController.orig +trap 'cp /tmp/HelloController.orig "$SRC"' EXIT + +# Print " " for every layer file in the tarball Jib wrote. +layers() { + rm -rf /tmp/jibtar && mkdir /tmp/jibtar && tar -xf target/jib-image.tar -C /tmp/jibtar + python3 - <<'PY' +import json, os, tarfile +d = "/tmp/jibtar" +m = json.load(open(f"{d}/manifest.json"))[0] +for name in m["Layers"][-5:]: # the five layers Jib added; the base image layers come first + t = tarfile.open(f"{d}/{name}") + files = [m.name for m in t.getmembers() if not m.isdir()] + print(name.replace(".tar.gz", "")[:12], f"{os.path.getsize(f'{d}/{name}'):>9} bytes", f"{len(files):>3} files", files[0]) +PY +} +{ + banner "Build 1: unchanged source" + "${MVN[@]}" -q clean compile jib:buildTar "${BASE_ARG[@]}" >/dev/null 2>&1 + layers | tee /tmp/layers1.txt + banner "Edit one string in HelloController.java, then build 2" + sed -i 's/hello from a Jib image/hello again/' "$SRC" + "${MVN[@]}" -q clean compile jib:buildTar "${BASE_ARG[@]}" >/dev/null 2>&1 + layers | tee /tmp/layers2.txt + banner "Layers whose digest changed (< build 1, > build 2)" + diff <(awk '{print $1}' /tmp/layers1.txt) <(awk '{print $1}' /tmp/layers2.txt) | grep '^[<>]' || true + banner "Files in the layer that changed" + NEW=$(diff <(awk '{print $1}' /tmp/layers1.txt) <(awk '{print $1}' /tmp/layers2.txt) | grep '^>' | head -1 | cut -c3- || true) + tar -tzf /tmp/jibtar/${NEW}*.tar.gz 2>/dev/null | grep -v '/$' +} 2>&1 | clean | tee "$OUT/04-layer-cache.txt" +cp /tmp/HelloController.orig "$SRC" diff --git a/jib/scripts/05-reproducible.sh b/jib/scripts/05-reproducible.sh new file mode 100755 index 0000000..30280b2 --- /dev/null +++ b/jib/scripts/05-reproducible.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +# Two builds of the same sources produce the same image digest. +source "$(dirname "$0")/lib.sh" +{ + banner "jib:buildTar twice, compare the image digest and id" + for n in 1 2; do + "${MVN[@]}" -q clean compile jib:buildTar "${BASE_ARG[@]}" >/dev/null 2>&1 + echo "build $n digest=$(cat target/jib-image.digest) id=$(cat target/jib-image.id)" + sha256sum target/jib-image.tar | cut -c1-20 | sed "s/^/build $n tar sha256 prefix=/" + sleep 2 + done + banner "Same build with creationTime=USE_CURRENT_TIMESTAMP" + for n in 1 2; do + "${MVN[@]}" -q clean compile jib:buildTar "${BASE_ARG[@]}" -Djib.container.creationTime=USE_CURRENT_TIMESTAMP >/dev/null 2>&1 + echo "build $n digest=$(cat target/jib-image.digest)" + sleep 2 + done +} 2>&1 | clean | tee "$OUT/05-reproducible.txt" diff --git a/jib/scripts/06-buildpacks.sh b/jib/scripts/06-buildpacks.sh new file mode 100755 index 0000000..832a08a --- /dev/null +++ b/jib/scripts/06-buildpacks.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# The same app through spring-boot:build-image (Cloud Native Buildpacks). +# Needs -Pcorp-proxy in the sandbox where the post was written, because the builder container has to +# reach github.com through a TLS-intercepting proxy. On an open network drop the PROFILE line. +source "$(dirname "$0")/lib.sh" +LOG=/tmp/bp-build.log +START=$(date +%s) +"${MVN[@]}" spring-boot:build-image -Dspring-boot.build-image.cleanCache=true "${BP_ARGS[@]}" >"$LOG" 2>&1 || { tail -30 "$LOG"; exit 1; } +END=$(date +%s) +{ + banner "mvn spring-boot:build-image (cold build: cleanCache=true; key lines of the build log)" + grep -E 'Building image|Using builder|\[creator\] +(Paketo Buildpack for (BellSoft|Spring Boot|Executable|CA|Syft|Java|Apache)|BellSoft Liberica JRE [0-9.]+: |Using Java version|Downloading from)|Adding layer|Successfully built|BUILD|Total time' "$LOG" | sed -E 's/^\[INFO\] +\[creator\] +/ /' | grep -v 'Finished at' + banner "Image config" + docker image inspect jib-demo:buildpacks --format 'tags={{.RepoTags}} +user={{.Config.User}} +created={{.Created}} +entrypoint={{json .Config.Entrypoint}} +layers={{len .RootFS.Layers}} +size={{.Size}} bytes' + banner "Wall time of this build (seconds, indicative)" + echo "$((END-START))" +} 2>&1 | clean | tee "$OUT/06-buildpacks.txt" diff --git a/jib/scripts/07-compare.sh b/jib/scripts/07-compare.sh new file mode 100755 index 0000000..ea84e5f --- /dev/null +++ b/jib/scripts/07-compare.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +# Jib vs Buildpacks on the same application: size, layers, rebuild time, runtime memory. Timings are indicative. +source "$(dirname "$0")/lib.sh" +SRC=src/main/java/com/ankurm/jibdemo/HelloController.java +cp "$SRC" /tmp/HelloController.orig +trap 'cp /tmp/HelloController.orig "$SRC"' EXIT +t() { local s=$SECONDS; "$@" >/tmp/compare-step.log 2>&1 || { tail -20 /tmp/compare-step.log; return 1; }; echo $((SECONDS-s)); } +{ + banner "Cold builds (Jib: local Jib cache removed; Buildpacks: cleanCache=true), seconds" + rm -rf ~/.cache/google-cloud-tools-java/jib target/jib-cache + echo "jib cold: $(t "${MVN[@]}" clean compile jib:dockerBuild "${BASE_ARG[@]}")" + echo "buildpacks cold: $(t "${MVN[@]}" clean spring-boot:build-image -Dspring-boot.build-image.cleanCache=true "${BP_ARGS[@]}")" + + banner "Warm rebuilds after a one-line change, seconds" + sed -i 's/hello from a Jib image/hello warm/' "$SRC" + echo "jib warm: $(t "${MVN[@]}" compile jib:dockerBuild "${BASE_ARG[@]}")" + echo "buildpacks warm: $(t "${MVN[@]}" spring-boot:build-image "${BP_ARGS[@]}")" + cp /tmp/HelloController.orig "$SRC" + "${MVN[@]}" -q compile jib:dockerBuild "${BASE_ARG[@]}" >/dev/null 2>&1 + "${MVN[@]}" -q spring-boot:build-image "${BP_ARGS[@]}" >/dev/null 2>&1 + + banner "Images (docker image inspect)" + for img in jib-demo:1.0.0 jib-demo:buildpacks; do + docker image inspect "$img" --format '{{index .RepoTags 0}} size={{.Size}} layers={{len .RootFS.Layers}} created={{.Created}} user={{.Config.User}}' + done + + banner "Is there a shell in the final image? (docker run --entrypoint sh ... -c 'echo yes')" + for img in jib-demo:1.0.0 jib-demo:buildpacks; do + printf '%-22s ' "$img"; docker run --rm --entrypoint sh "$img" -c 'echo yes' 2>&1 | head -1 | cut -c1-260 || true + done + + banner "Run each image under a memory limit" + run_case() { # run_case [extra docker args...] + local img=$1 mem=$2; shift 2 + docker rm -f cmp >/dev/null 2>&1 || true + docker run -d --name cmp -m "$mem" -p 18081:8080 "$@" "$img" >/dev/null + for i in $(seq 1 60); do curl -s -o /dev/null localhost:18081/actuator/health && break; \ + [ "$(docker inspect -f '{{.State.Running}}' cmp)" = true ] || break; sleep 1; done + echo "--- $img memory=$mem $*" + if [ "$(docker inspect -f '{{.State.Running}}' cmp)" = true ]; then + curl -s localhost:18081/hello; echo + docker stats --no-stream --format 'container memory in use: {{.MemUsage}}' cmp + else + echo "container exited with code $(docker inspect -f '{{.State.ExitCode}}' cmp)" + fi + docker logs cmp 2>&1 | grep -E 'Memory Configuration|Setting Active Processor|unable to calculate|fixed memory|Started JibDemoApplication' | sed -E 's/^[0-9T:.Z-]+ +//' | cut -c1-260 + docker rm -f cmp >/dev/null 2>&1 || true + } + run_case jib-demo:1.0.0 512m + run_case jib-demo:buildpacks 512m + run_case jib-demo:buildpacks 512m -e BPL_JVM_THREAD_COUNT=50 + run_case jib-demo:buildpacks 1g +} 2>&1 | clean | tee "$OUT/07-compare.txt" +cp /tmp/HelloController.orig "$SRC" diff --git a/jib/scripts/08-failures.sh b/jib/scripts/08-failures.sh new file mode 100755 index 0000000..aaec5bb --- /dev/null +++ b/jib/scripts/08-failures.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# The ways a Jib build or a Jib image goes wrong, reproduced. +source "$(dirname "$0")/lib.sh" +J21=public.ecr.aws/docker/library/eclipse-temurin:21-jre # Java 21 JRE from the ECR Public mirror (Docker Hub was rate-limiting) +{ + banner "1. jib:buildTar without compiling first (target/ was just cleaned)" + echo '$ mvn clean jib:buildTar' + "${MVN[@]}" clean jib:buildTar "${BASE_ARG[@]}" 2>&1 | grep -E 'ERROR\] Failed' | sed -E 's#/[^ ]*/jib/target#/target#' | cut -c1-420 || true + + banner "2. Base image older than the compiled bytecode (class files are Java 25, JRE is 21)" + "${MVN[@]}" -q compile jib:dockerBuild -Djib.from.image=docker://$J21 -Dimage.name=jib-demo-wrongjre >/dev/null 2>&1 + echo '$ docker run jib-demo-wrongjre' + docker run --rm jib-demo-wrongjre 2>&1 | grep -E 'Error|Exception|class file version|Unsupported' | cut -c1-300 | head -3 || true + docker rmi jib-demo-wrongjre:1.0.0 jib-demo-wrongjre:latest >/dev/null 2>&1 || true + + banner "3. Heap size at a 512 MB container limit: JVM default vs -XX:MaxRAMPercentage=75" + for pct in 25 75; do + echo "MaxRAMPercentage=$pct" + docker run --rm -m 512m --entrypoint java jib-demo:1.0.0 -XX:MaxRAMPercentage=$pct -XX:+PrintFlagsFinal -version 2>/dev/null | grep -E ' MaxHeapSize ' | sed -E 's/ +/ /g' + done + + banner "4. containerizingMode=packaged after mvn package (Spring Boot repackaged the jar)" + "${MVN[@]}" -q clean package -DskipTests >/dev/null 2>&1 + ls -l target/jib-demo-1.0.0.jar target/jib-demo-1.0.0.jar.original | awk '{print $5, $9}' + "${MVN[@]}" -q jib:dockerBuild -Djib.containerizingMode=packaged -Dimage.name=jib-demo-packaged "${BASE_ARG[@]}" >/dev/null 2>&1 + echo '$ docker run --rm --entrypoint ls -l /app/classpath' + docker run --rm --entrypoint ls jib-demo-packaged -l /app/classpath | awk 'NR>1{print $5, $9}' + docker rm -f packaged >/dev/null 2>&1 || true + docker run -d --name packaged jib-demo-packaged >/dev/null + sleep 15 + docker logs packaged 2>&1 | grep -E 'Started JibDemoApplication' | sed -E 's/^[0-9T:.Z-]+ +//' | cut -c1-200 || true + docker rm -f packaged >/dev/null 2>&1 || true + docker rmi jib-demo-packaged:1.0.0 jib-demo-packaged:latest >/dev/null 2>&1 || true +} 2>&1 | clean | tee "$OUT/08-failures.txt" diff --git a/jib/scripts/09-registry-push.sh b/jib/scripts/09-registry-push.sh new file mode 100755 index 0000000..026da30 --- /dev/null +++ b/jib/scripts/09-registry-push.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# jib:build: build and push straight to a registry, with no Docker daemon involved in the build. +# The base image comes from a registry too (ECR Public mirror of Docker Official Images), so the +# whole pipeline is registry -> Jib -> registry. +source "$(dirname "$0")/lib.sh" +BASE_REG=public.ecr.aws/docker/library/eclipse-temurin:25-jre +docker rm -f local-registry >/dev/null 2>&1 || true +docker run -d --name local-registry -p 5000:5000 registry:2 >/dev/null +sleep 2 +{ + banner "mvn jib:build to localhost:5000 (base image pulled from $BASE_REG)" + echo '$ mvn compile jib:build -Dimage.name=localhost:5000/jib-demo -Djib.allowInsecureRegistries=true' + "${MVN[@]}" compile jib:build -Dimage.name=localhost:5000/jib-demo -Djib.allowInsecureRegistries=true -Djib.from.image=$BASE_REG 2>&1 \ + | sed -n '/--- jib:/,$p' | grep -E 'Containerizing|Getting manifest|Building|Pushing|Using base|Built and pushed|BUILD' | sed -E 's/with digest: sha256:([0-9a-f]{12})[0-9a-f]+/with digest: sha256:\1.../' + banner "What the registry now holds" + echo '$ curl localhost:5000/v2/_catalog'; curl -s localhost:5000/v2/_catalog; echo + echo '$ curl localhost:5000/v2/jib-demo/tags/list'; curl -s localhost:5000/v2/jib-demo/tags/list; echo + echo '$ manifest media type and layer count' + curl -s -H 'Accept: application/vnd.oci.image.manifest.v1+json, application/vnd.docker.distribution.manifest.v2+json' localhost:5000/v2/jib-demo/manifests/1.0.0 \ + | python3 -c 'import sys,json; m=json.load(sys.stdin); print(m["mediaType"], "layers:", len(m["layers"]))' + banner "Pull and run what Jib pushed" + docker rmi localhost:5000/jib-demo:1.0.0 >/dev/null 2>&1 || true + docker pull -q localhost:5000/jib-demo:1.0.0 >/dev/null + docker run --rm --entrypoint sh localhost:5000/jib-demo:1.0.0 -c 'cat /etc/motd.txt' +} 2>&1 | clean | tee "$OUT/09-registry-push.txt" +docker rm -f local-registry >/dev/null 2>&1 || true diff --git a/jib/scripts/10-default-base.sh b/jib/scripts/10-default-base.sh new file mode 100755 index 0000000..74a2b49 --- /dev/null +++ b/jib/scripts/10-default-base.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +# Which base image does Jib choose when you do not say? Ask it, with no -Djib.from.image override. +source "$(dirname "$0")/lib.sh" +{ + banner "mvn compile jib:buildTar with no base-image override (java.version=25 in the pom)" + echo '$ mvn compile jib:buildTar' + "${MVN[@]}" clean compile jib:buildTar 2>&1 | grep -E 'Getting manifest|Using base image|Built image|ERROR\] (Failed|GET|\{)|BUILD' | sed -E 's/with digest: sha256:([0-9a-f]{12})[0-9a-f]+/with digest: sha256:\1.../' | cut -c1-300 || true +} 2>&1 | clean | tee "$OUT/10-default-base.txt" diff --git a/jib/scripts/lib.sh b/jib/scripts/lib.sh new file mode 100755 index 0000000..8c8a606 --- /dev/null +++ b/jib/scripts/lib.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# Shared setup for the scenario scripts. Source it; do not run it. +set -euo pipefail + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +MODULE="$(cd "$HERE/.." && pwd)" +OUT="$MODULE/output" +mkdir -p "$OUT" +cd "$MODULE" + +# JDK: use JAVA_HOME if the caller set it (the post was tested with Temurin 25.0.5.1+2). +export PATH="${JAVA_HOME:+$JAVA_HOME/bin:}$PATH" + +# Base image source. "docker://" makes Jib read the base from the local Docker daemon instead of +# Docker Hub. The sandbox used for the post hit Docker Hub's anonymous pull limit (HTTP 429), so +# the scripts default to the daemon copy. Set JIB_BASE="" to let Jib pull eclipse-temurin:25-jre +# from the registry (Jib's default for Java 25). +JIB_BASE="${JIB_BASE-docker://eclipse-temurin:25-jre}" +BASE_ARG=() +[ -n "$JIB_BASE" ] && BASE_ARG=("-Djib.from.image=$JIB_BASE") + +MVN=(mvn -B -ntp) + +# Strip JVM-launcher noise and wall-clock lines that would make every rerun diff. +clean() { grep -v -E '^Picked up|^WARNING:|Finished at:|^\[INFO\] *$' ; } + +banner() { printf '\n=== %s ===\n' "$*"; } + +# Buildpacks arguments. In the sandbox the builder container needs the proxy and its CA (-Pcorp-proxy); +# on an open network leave PROXY_URL unset and these stay empty. +BP_ARGS=(-DskipTests -Dspring-boot.build-image.pullPolicy=IF_NOT_PRESENT) +if [ -n "${PROXY_URL:-}" ]; then + BP_ARGS+=(-Pcorp-proxy "-Dproxy.url=$PROXY_URL" "-Dca.dir=${CA_DIR:?set CA_DIR to a folder holding type + ca.crt}") +fi diff --git a/jib/scripts/run-all.sh b/jib/scripts/run-all.sh new file mode 100755 index 0000000..8f84e76 --- /dev/null +++ b/jib/scripts/run-all.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +# Regenerate every file in output/. Needs: JDK 25, Maven 3.9+, a running Docker daemon. +# export JAVA_HOME=/path/to/jdk-25 +# ./scripts/run-all.sh # open network +# PROXY_URL=http://host:port CA_DIR=/dir/with/type+ca.crt ./scripts/run-all.sh # TLS-intercepting proxy (see pom profile corp-proxy) +# Timings in 07-compare.txt vary from run to run; treat them as indicative. +set -euo pipefail +cd "$(dirname "$0")" +for s in 01-versions 02-jib-build 03-run 04-layer-cache 05-reproducible 06-buildpacks 07-compare 08-failures 09-registry-push 10-default-base; do + echo ">> $s"; ./$s.sh >/dev/null +done +echo "done: $(ls ../output | wc -l) files in output/" diff --git a/jib/src/main/java/com/ankurm/jibdemo/HelloController.java b/jib/src/main/java/com/ankurm/jibdemo/HelloController.java new file mode 100644 index 0000000..4dec848 --- /dev/null +++ b/jib/src/main/java/com/ankurm/jibdemo/HelloController.java @@ -0,0 +1,22 @@ +package com.ankurm.jibdemo; + +import java.util.Map; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloController { + + /** Change GREETING and rebuild to watch exactly one Jib layer change (scripts/04-layer-cache.sh). */ + static final String GREETING = "hello from a Jib image"; + + @GetMapping("/hello") + public Map hello() { + return Map.of( + "greeting", GREETING, + "javaVersion", Runtime.version().toString(), + "maxHeapMb", Runtime.getRuntime().maxMemory() / (1024 * 1024), + "user", System.getProperty("user.name")); + } +} diff --git a/jib/src/main/java/com/ankurm/jibdemo/JibDemoApplication.java b/jib/src/main/java/com/ankurm/jibdemo/JibDemoApplication.java new file mode 100644 index 0000000..5cb5ab7 --- /dev/null +++ b/jib/src/main/java/com/ankurm/jibdemo/JibDemoApplication.java @@ -0,0 +1,12 @@ +package com.ankurm.jibdemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class JibDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(JibDemoApplication.class, args); + } +} diff --git a/jib/src/main/jib/etc/motd.txt b/jib/src/main/jib/etc/motd.txt new file mode 100644 index 0000000..904e785 --- /dev/null +++ b/jib/src/main/jib/etc/motd.txt @@ -0,0 +1 @@ +jib-demo image: this file came from src/main/jib/etc/motd.txt via extraDirectories diff --git a/jib/src/main/resources/application.properties b/jib/src/main/resources/application.properties new file mode 100644 index 0000000..6f4f97f --- /dev/null +++ b/jib/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.application.name=jib-demo +management.endpoints.web.exposure.include=health