Add jib: Spring Boot 4.2.0 images with Jib 3.5.2 vs Buildpacks

Same app built both ways: layers, reproducible digest, extraDirectories, registry push
without a daemon, wrong-JRE and missing-compile failures, Buildpacks memory calculator at
512 MB. Captured output in jib/output, regenerated by jib/scripts/run-all.sh.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b6ZEVHGaYy15HAxRzy
This commit is contained in:
Ankur Mhatre
2026-12-01 08:32:09 +05:30
co-authored by Claude Sonnet 5.5
parent ddc3ec9d90
commit bb12eaca0a
30 changed files with 744 additions and 0 deletions
+13
View File
@@ -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 '<version>4\.2\.0</version>|<jib.version>|<java.version>' pom.xml | sed 's/^ *//'
banner "Base image used for Jib (digest of the local copy)"
echo "JIB_BASE=${JIB_BASE:-<registry default>}"
docker image inspect eclipse-temurin:25-jre --format 'id={{.Id}}'
} 2>&1 | clean | tee "$OUT/01-versions.txt"
+20
View File
@@ -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"
+16
View File
@@ -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
+35
View File
@@ -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 "<layer digest prefix> <size in bytes>" 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"
+18
View File
@@ -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"
+22
View File
@@ -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"
+54
View File
@@ -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 <image> <memory> [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"
+34
View File
@@ -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#<module>/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"
+26
View File
@@ -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
+8
View File
@@ -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"
+34
View File
@@ -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
+12
View File
@@ -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/"