Choosing a base image
40 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)ShopLite's first image started from a full JDK and was far bigger than it needed to be. The base image you pick decides the size, the Java version, the security fixes you inherit and how the JVM sizes its memory.
JDK vs JRE images: build with a JDK, run with a JRE
A JRE image holds the Java runtime and nothing else needed to run compiled code. A JDK image adds the developer tools, such as javac, jshell and jar. Compiling needs a JDK; running a jar needs only a JRE.
The health server below is compiled on your computer with javac --release 17, then copied into a JRE image that can only run it.
Example 1 · The health server on a JRE image
Runs in: a terminal with Docker installed
# DockerfileFROM eclipse-temurin:25-jreWORKDIR /appCOPY HealthServer.class .EXPOSE 8080CMD ["java", "HealthServer"]# Terminal: compile on your computer, then build and comparejavac --release 17 HealthServer.javadocker build -t shoplite-hello:0.2 .docker images shoplite-helloREPOSITORY TAG IMAGE ID CREATED SIZE shoplite-hello 0.2 a71d3e0c55b9 10 seconds ago 268MB shoplite-hello 0.1 5c0e8a3f91d2 2 hours ago 452MB
What this code does
- The
.classfile is compiled on your computer, so the image only needs to run it:CMD ["java", "HealthServer"]names the class, not the source file. - The same program in a JRE image is about 180 MB smaller than the JDK version from the previous lesson. Sizes depend on your Docker version and CPU platform.
The JRE image is roughly 180 MB smaller, downloads faster on every server and contains fewer programs that could have security bugs. An attacker who gets into a JRE container also finds no compiler to build tools with.
So the rule is: build with a JDK, run with a JRE. The Multi-stage builds lesson moves the build step into Docker.
Find the bug
Spot the wrong base image
Tap the line that makes the final image worse.
This Dockerfile only runs the finished ShopLite jar. Which line is the problem?
Round 1 of 2java HealthServer.java (source-file mode), because that mode compiles the file first. Copy the compiled .class file or a jar instead.eclipse-temurin variants: Ubuntu, Alpine and distroless
A distroless image holds only Java and its runtime files: no shell and no package manager. The attack surface is small, but you cannot docker exec into a shell to debug.
eclipse-temurin is the Docker Official Image of the Eclipse Adoptium OpenJDK builds. Its tags combine the Java version, JDK or JRE, and the operating system. The table below compares their sizes.
Example 2 · Comparing base image sizes
Runs in: a terminal with Docker installed
docker pull eclipse-temurin:25-jdkdocker pull eclipse-temurin:25-jredocker pull eclipse-temurin:25-jre-alpinedocker pull gcr.io/distroless/java21-debian12docker images --format "table {{.Repository}}:{{.Tag}}\t{{.Size}}"REPOSITORY:TAG SIZE eclipse-temurin:25-jdk 452MB eclipse-temurin:25-jre 268MB eclipse-temurin:25-jre-alpine 176MB gcr.io/distroless/java21-debian12 192MB
What this code does
- Rough sizes on linux/amd64: the JDK is the largest, the Ubuntu JRE is the usual choice, and Alpine and distroless are smaller again. Check your own numbers with
docker images; they change with every update. - The distroless line uses Java 21 because that is a version Google publishes for Debian 12; check the distroless project for newer Java versions before choosing it.
- The
--formatoption with{{.Repository}}:{{.Tag}}prints just the columns you ask for, which keeps the comparison short.
Pick the variant by what you need:
25-jre(Ubuntu, the default): glibc, a shell andapt. Works with almost every library; the safe first choice.25-jre-alpine: built on Alpine Linux, a tiny distribution that uses musl instead of glibc. About 90 MB smaller, but some native libraries expect glibc, and fewer CPU platforms are published.- Distroless (
gcr.io/distroless/java21-debian12): new Java versions can arrive later than on Temurin. Canonical's chiselledubuntu/jreimages follow the same "no shell" idea on Ubuntu.
For ShopLite the course uses eclipse-temurin:25-jre: small enough, supports every platform and keeps a shell for debugging while you learn.
Pinning: 25-jre vs latest vs digest
A digest is a checksum of one exact image, written @sha256: and 64 hex characters. It never changes, so builds repeat byte for byte, but you must update it yourself to get security fixes.
A tag such as 25-jre is a movable name: the publisher can point it at a new image at any time. The commands below look up the digest of one exact image.
Example 3 · Finding and pinning a digest
Runs in: a terminal with Docker installed
docker buildx imagetools inspect eclipse-temurin:25-jreName: docker.io/library/eclipse-temurin:25-jre MediaType: application/vnd.oci.image.index.v1+json Digest: sha256:4b6f0c9e2d7a51c8e3f09b6a1d2c4e5f60718293a4b5c6d7e8f9012345678abc Manifests: Name: docker.io/library/eclipse-temurin:25-jre@sha256:91c2… MediaType: application/vnd.oci.image.manifest.v1+json Platform: linux/amd64 Name: docker.io/library/eclipse-temurin:25-jre@sha256:0e7d… MediaType: application/vnd.oci.image.manifest.v1+json Platform: linux/arm64
What this code does
- The top
Digestbelongs to the image index, a list that points to one image per CPU platform. Pin that one, so the sameFROMline works on both Intel/AMD (amd64) and ARM (arm64) machines. - The pinned line is
FROM eclipse-temurin:25-jre@sha256:4b6f0c9e2d7a51c8e3f09b6a1…with the full 64 hex characters. Your digest will differ; it changes every time the image is rebuilt. - Keeping the tag before
@sha256:is optional for Docker, but it tells humans which version the digest belongs to.
Three ways to choose a base, from loosest to strictest:
eclipse-temurinor:latest: never use these. One day the name means a newer Java major version, and a build that worked yesterday fails today.eclipse-temurin:25-jre: stays on Java 25 but picks up security updates when you rebuild. A good default for most teams.eclipse-temurin:25-jre@sha256:...: pinned to the digest. Exact version tags such as25.0.1_8-jrecan still be re-pushed when packages inside are patched; only a digest is fixed forever.
Trust and updates: official images and docker scout
A CVE (Common Vulnerabilities and Exposures entry) is a publicly listed security bug in some package. Scanners such as docker scout match the packages in your image against CVE lists and count them by severity: C, H, M and L.
Everything in the base image ends up in your image and runs with your app. The scan below checks the shoplite-api image for known security bugs.
Example 4 · docker scout quickview on shoplite-api
Runs in: a terminal with Docker installed and a free Docker account
docker logindocker scout quickview shoplite-api:1.2.0 ✓ Image stored for indexing
✓ Indexed 214 packages
Target │ shoplite-api:1.2.0 │ 0C 1H 3M 12L
digest │ 7d41c2a9e0f3 │
Base image │ eclipse-temurin:25-jre │ 0C 0H 2M 10L
Updated base image │ eclipse-temurin:25-jre │ 0C 0H 1M 9L
│ │ -1 -1What this code does
- The target has 1 high CVE but the base image has none, so that bug comes from something you added, usually a library inside the jar. Fix it by updating the dependency (often a newer Spring Boot version) and rebuilding.
Updated base imageshows a newer build of the same tag with fewer problems (-1medium,-1low).docker build --pullpicks it up.- Counts change every week as new CVEs are published; the point is the habit of checking, not these numbers.
Prefer an official image, published under Docker Hub's library namespace (docker.io/library/eclipse-temurin) and reviewed by Docker, over a random user's image.
New CVEs appear all the time, so an image that was clean last month may not be clean today. Rebuild regularly with docker build --pull, which looks for a newer base image instead of reusing the one already on your computer.
JVM in a container: MaxRAMPercentage via JAVA_TOOL_OPTIONS
MaxRAMPercentage (-XX:MaxRAMPercentage=75) is a JVM option that sets the maximum heap as a percentage of the container memory limit. The default percentage is 25, and a percentage follows the limit when a server changes it.
Java 10 and newer is container-aware: the JVM reads the container's memory limit and gives the heap 25% of it. The Dockerfile below raises that to 75%.
FROM eclipse-temurin:25-jreWORKDIR /appCOPY target/*.jar app.jarENV JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=75"EXPOSE 8080ENTRYPOINT ["java", "-jar", "/app/app.jar"]Example 5 · What the JVM sees inside a container
Runs in: a terminal with Docker installed
public class ContainerJvmDemo { public static void main(String[] args) { Runtime rt = Runtime.getRuntime(); long mb = 1024 * 1024; System.out.println("Java version : " + Runtime.version().feature()); System.out.println("CPUs the JVM sees: " + rt.availableProcessors()); System.out.println("Max heap : " + rt.maxMemory() / mb + " MB"); }}Java version : 25 CPUs the JVM sees: 1 Max heap : 123 MB Picked up JAVA_TOOL_OPTIONS: -XX:MaxRAMPercentage=75 Java version : 25 CPUs the JVM sees: 1 Max heap : 371 MB
What this code does
- Run it in a 512 MB, 1 CPU container from the folder that holds the file:
docker run --rm -m 512m --cpus 1 -v "$PWD":/app -w /app eclipse-temurin:25-jdk java ContainerJvmDemo.java, then again with-e JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=75". In PowerShell write${PWD}instead of$PWD. - With the default 25% the heap is about 128 MB of the 512 MB limit; with 75% it is about 384 MB.
maxMemory()reports a little less than the setting, because with one CPU the JVM picks the Serial collector and leaves out one survivor space. availableProcessors()returns 1 because of--cpus 1, even if your laptop has 12 cores. Your numbers depend on your machine and Java build.
Example 6 · Planning heap size for a container limit
public class HeapPlanDemo { // Simplified model: heap = container limit x MaxRAMPercentage / 100 static int heapMb(int limitMb, double percent) { return (int) (limitMb * percent / 100); }
public static void main(String[] args) { int[] limits = {512, 1024, 2048}; System.out.println("Limit Default 25% MaxRAMPercentage=75 Left for non-heap"); for (int limit : limits) { int small = heapMb(limit, 25); int big = heapMb(limit, 75); System.out.printf("%4d MB %6d MB %8d MB %6d MB%n", limit, small, big, limit - big); } }}Limit Default 25% MaxRAMPercentage=75 Left for non-heap 512 MB 128 MB 384 MB 128 MB 1024 MB 256 MB 768 MB 256 MB 2048 MB 512 MB 1536 MB 512 MB
What this code does
- A simplified model of the JVM rule: heap = limit × percentage / 100. Real values are slightly lower, as Example 5 shows.
- With the default 25%, a 512 MB container gives Spring Boot only 128 MB of heap, which is tight for an API with Hibernate. With 75% the same container gives 384 MB and still leaves 128 MB for non-heap memory.
25% is cautious for a container that runs only one Java app. A higher percentage leaves the rest for memory outside the heap: loaded classes, thread stacks, the JIT compiler's code and network buffers. Use a percentage, not -Xmx, so the heap follows whatever limit the server sets.
Put the option in JAVA_TOOL_OPTIONS, an environment variable that every JVM reads at start. Set it with ENV in the Dockerfile as a default, or with -e on docker run. The JVM prints Picked up JAVA_TOOL_OPTIONS: ... on stderr, which confirms it was read.
Predict the output
How big is the heap?
The JVM sizes its heap from the container memory limit.
Round 1 of 2
docker run -m 1g, no JVM options. What is the default maximum heap?
docker ps -a shows exit code 137. Raise the limit or lower the percentage; do not remove the limit.Next step
Check your understanding
11 quick questions