Docker Image with JDK 8 for Legacy CI Builds
Legacy Java 8 services still need to compile and test, but the base images many pipelines inherited are no longer maintained. A Docker image with JDK 8 for a legacy CI build should be small, pinned, and predictable: an explicit JAVA_HOME, a known JDK patch level, a non-root user, and a toolchain you can verify before the build runs.
This tutorial builds that image from Eclipse Temurin, pins it by digest, adds Maven with a checked download, and verifies java, javac, and the real build command inside the container. It also covers the Jenkins agent detail that trips up many migrations: the agent JVM and the job JDK are not the same thing.
Table of Contents
- Why old JDK 8 images break builds
- Choose a maintained JDK 8 base and architecture
- Write the pinned multi-stage Dockerfile
- Verify the toolchain inside the image
- Pin, update, and audit the image
- Reproduce the JDK 8 toolchain locally
- Jenkins agents: keep the agent JVM separate
- Conclusion
Why old JDK 8 images break builds
Start with the image reference, not the build script. If a Dockerfile still says FROM java:8 or FROM openjdk:8, it is building on an unmaintained image line. The Docker Hub page for the openjdk official image marks the image as officially deprecated and explains that only Early Access tags continue to receive updates after July 2022. That is not a safe source of JDK 8 security fixes.
The second problem is mutability. Even a maintained tag such as 8-jdk-jammy is a moving pointer: the publisher can rebuild the same tag with a different base OS, CA bundle, locale data, or JDK patch release. Docker's build best practices explain that tags are mutable and that a digest pins the exact image content, even if the publisher later moves the tag.
That difference matters for legacy pipelines. A rebuild six months after the original image was tested can quietly change the compiler patch level or the glibc and certificate stack. The build may fail for reasons unrelated to the application change, and the failure is hard to reproduce because the image reference looks unchanged.
| Image reference | Status | What it means for CI |
|---|---|---|
java:8 | Retired | No JDK 8 security updates; replace it |
openjdk:8 | Deprecated | Only Early Access tags updated after July 2022 |
eclipse-temurin:8-jdk-jammy | Maintained | Official image, but the tag still moves |
eclipse-temurin:8-jdk-jammy@sha256:... | Pinned | Reproducible until you deliberately update it |
A maintained and pinned base is the foundation. The rest of this article chooses that base and then hardens the build image around it.
Choose a maintained JDK 8 base and architecture
Eclipse Temurin is the OpenJDK build published by the Eclipse Adoptium project and distributed as a Docker Official Image. The Temurin Docker Hub page documents the supported tags, variants, and platforms.
Adoptium's own Temurin support roadmap lists Java 8 as available at least through December 2032, with maintenance and security updates following upstream OpenJDK's quarterly cadence. That is a much better support story than an abandoned community image, but it still requires you to pick up releases deliberately.
Read the tag before you copy it
A tag such as 8-jdk-jammy carries three pieces of information:
-
8: the Java major version. -
jdk: the full development kit, includingjavac. A JRE image can run Java but cannot compile it. -
jammy: Ubuntu 22.04. The OS name matters because it determines the C library, package versions, and available locales.
Floating aliases such as 8-jdk and 8-jdk-jammy are convenient, but they are exactly the moving tags described earlier. A version-specific tag such as 8u<update>-b<build>-jdk-jammy is easier for reviewers to reason about, but the digest is what makes the image reproducible. Even a floating tag plus a digest is pinned; the tag is just a human-readable label.
For most legacy Maven, Ant, and Gradle builds, the Ubuntu Jammy variant is the safer default. The Temurin page notes that the Alpine variant uses musl libc, which can break tools and native libraries that assume glibc. Alpine's smaller size is attractive, but a surprising native-library failure in a legacy build costs more than the saved megabytes.
Before adopting any base, check its default user. Some JDK 8 images run as root by default, so plan to add a non-root USER even if you choose a different publisher.
Choose the architecture deliberately
The Temurin image page lists support for amd64, arm32v7, arm64v8, ppc64le, riscv64, s390x, and windows-amd64. On a normal x86-64 CI runner, use linux/amd64. On Apple silicon or arm64 runners, use linux/arm64.
If you build for more than one architecture, pin the top-level index digest so Docker can select the matching platform manifest. If you need one specific architecture, pin that platform's manifest digest instead. The command later in this article shows how to inspect both.
Temurin's JDK 8 Ubuntu Dockerfile sets JAVA_HOME=/opt/java/openjdk, puts $JAVA_HOME/bin first on PATH, and runs java -version and javac -version as part of the image build. We will keep the same JAVA_HOME path and add our own explicit checks so the image fails at build time if the toolchain is broken.
Write the pinned multi-stage Dockerfile
The Dockerfile below has two stages. The first downloads and verifies Maven. The second copies only the verified Maven directory into the final image, creates a non-root user, and checks the toolchain as that user. Docker's build guidance recommends multi-stage builds for this kind of separation, because build-only artifacts and temporary archives do not belong in the final image.
# syntax=docker/dockerfile:1 ARG TEMURIN_IMAGE=eclipse-temurin:8-jdk-jammy@sha256:d47056146401dbffde28978d4ccb2d54dbcf52935881a6693f137bb92bfca63e ARG MAVEN_VERSION=3.10.2 ARG MAVEN_SHA512=57d2db6b37e7582119137ade544557bba741b29e62109ef81539a2bdfd6d0e641fdd5d8edb014af1816d0510deac6fac8f347433b3a1aa36316ba85aa77b8119 FROM ${TEMURIN_IMAGE} AS maven ARG MAVEN_VERSION ARG MAVEN_SHA512 RUN curl --fail --silent --show-error --location \ --output /tmp/apache-maven.tar.gz \ "https://archive.apache.org/dist/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz" \ && echo "${MAVEN_SHA512} /tmp/apache-maven.tar.gz" | sha512sum --check --status \ && mkdir -p /opt/maven \ && tar --extract --gzip --file /tmp/apache-maven.tar.gz \ --directory /opt/maven --strip-components=1 \ && rm -f /tmp/apache-maven.tar.gz FROM ${TEMURIN_IMAGE} AS ci ARG CI_UID=10001 ARG CI_GID=10001 ENV JAVA_HOME=/opt/java/openjdk \ MAVEN_HOME=/opt/maven \ HOME=/home/ci \ PATH="/opt/maven/bin:/opt/java/openjdk/bin:${PATH}" COPY --from=maven /opt/maven /opt/maven RUN groupadd --gid "${CI_GID}" ci \ && useradd --uid "${CI_UID}" --gid "${CI_GID}" \ --create-home --shell /bin/bash ci \ && mkdir -p /workspace /home/ci/.m2 \ && chown -R "${CI_UID}:${CI_GID}" /workspace /home/ci USER ci RUN java -version \ && javac -version \ && mvn -version WORKDIR /workspace CMD ["/bin/bash"] A few details are worth calling out:
- The base is pinned twice, in effect. The readable tag says
8-jdk-jammy; the digest says exactly which image that tag resolved to. The digest above is an index digest, so Docker picks the right platform for the runner. Refresh it before you rely on it, as shown in the update section below. - Maven is pinned and checked.
MAVEN_VERSIONandMAVEN_SHA512are build arguments, so a version bump is a one-line change. The checksum is verified before extraction, and a mismatch fails the build before Maven ever runs. The Apache Maven download page lists the current release and its checksum; Maven 3.10.2 requires JDK 8 or later to execute. If the project is validated against an older Maven line, pin that version and its checksum instead of changing the build tool as part of the container change. -
JAVA_HOMEandPATHare explicit. The image does not depend on a login shell to findjavaorjavac.PATHputs Maven'sbindirectory first, then the pinned JDK. - The build runs as a fixed non-root user. UID and GID 10001 avoid the ambiguity of a dynamically assigned user, and
/workspaceand the Maven local repository are owned by that user. Docker's own guidance recommends an explicit UID/GID and a non-rootUSERwhen the process does not need privileges. - The verification runs as the same user that will run CI. If
java,javac, ormvnonly works as root, the image build fails now rather than during a pipeline. - The command is a shell. This is a build environment, not a long-running service. The base image's CA-certificate entrypoint is inherited, but it simply executes the command you pass when no extra certificates are requested.
If your CI system requires a different UID, override CI_UID and CI_GID at build time and make sure the workspace ownership matches. The next step is to prove the image actually contains the toolchain you expect.
Verify the toolchain inside the image
Build the image and run a short verification script. The script fails on the first mismatch, which makes it suitable for a CI smoke test.
docker build -t legacy-jdk8-ci:local . If the runner's default platform is not the one you want, add --platform linux/amd64 or --platform linux/arm64. The index digest lets Docker select the matching manifest for that platform.
docker run --rm legacy-jdk8-ci:local bash -c ' set -euo pipefail test "$(id -u)" -eq 10001 test "$JAVA_HOME" = "/opt/java/openjdk" java -version javac -version mvn -version readlink -f "$(command -v javac)" ' The output should look like this, with the patch release matching the base image you pinned:
| Check | Expected result |
|---|---|
id -u | 10001 |
$JAVA_HOME | /opt/java/openjdk |
java -version | openjdk version "1.8.0_...", with Temurin in the runtime line |
javac -version | javac 1.8.0_... |
mvn -version | Apache Maven 3.10.2 and Java version: 1.8.0_... |
readlink -f "$(command -v javac)" | /opt/java/openjdk/bin/javac |
Now run the command your pipeline actually uses, not just the version checks. For a Maven project:
docker run --rm \ -v "$PWD":/workspace \ -w /workspace \ legacy-jdk8-ci:local \ mvn -B -ntp -DskipTests package On a workstation, the checkout is usually owned by your user, while the container runs as UID 10001. Add --user and mount your Maven cache so the container can write the workspace and reuse dependencies:
mkdir -p "$HOME/.m2" docker run --rm \ --user "$(id -u):$(id -g)" \ -v "$PWD":/workspace \ -v "$HOME/.m2":/home/ci/.m2 \ -w /workspace \ legacy-jdk8-ci:local \ mvn -B -ntp -DskipTests package In CI, make the workspace writable by the container UID, or run the container with the CI user's UID. In Kubernetes, an fsGroup can handle group ownership for mounted volumes. If your project uses the Gradle wrapper, run ./gradlew --version and ./gradlew build with the wrapper version the project already pins; changing the wrapper is a separate migration.
Pin, update, and audit the image
Pinning a digest solves reproducibility, but it also opts you out of automatic base image updates. The maintenance plan has to be explicit.
First, inspect the current tag and copy the digest you need:
docker buildx imagetools inspect eclipse-temurin:8-jdk-jammy The output shows a top-level Digest for the multi-platform index and a digest for each platform manifest. Use the index digest when you want one Dockerfile to work on both amd64 and arm64; use a platform manifest digest when you need a strict single-architecture pin.
Then keep the update path short:
- Track the JDK. Temurin ships security updates on the upstream quarterly cadence. Revisit the digest after each release, or sooner if Adoptium publishes a critical security update.
- Update the digest, not just the tag. Change
TEMURIN_IMAGEin one place. Keep the previous digest in your deployment history so you can roll back. - Update Maven separately. Change
MAVEN_VERSIONandMAVEN_SHA512together. A stale checksum will fail the build before Maven runs, which is the desired behavior. - Rebuild and verify. Run the verification script and the real project build on a branch before promoting the image.
- Record what is inside. Read
JAVA_VERSIONfrom the running image withdocker run --rm legacy-jdk8-ci:local printenv JAVA_VERSION, and keep that value in the image label or release notes.
Docker's best practices also recommend automating this with Dependabot's Docker ecosystem support. A small configuration file is enough:
# .github/dependabot.yml version: 2 updates: - package-ecosystem: "docker" directory: "/" schedule: interval: "weekly" Do not treat docker build --pull as your update mechanism when the base is pinned by digest. --pull refreshes tags, but a digest still resolves to the same content. To pick up a new JDK release, change the digest deliberately, rebuild, and run the same checks.
Reproduce the JDK 8 toolchain locally
When a build fails only in CI, the fastest path is usually to reproduce the toolchain on a workstation. Install Temurin 8 from Adoptium's package repository; the Adoptium Linux installation instructions show the repository setup and the temurin-8-jdk package name.
sudo apt update sudo apt install temurin-8-jdk export JAVA_HOME=/usr/lib/jvm/temurin-8-jdk-amd64 export PATH="$JAVA_HOME/bin:$PATH" java -version javac -version On arm64, the directory usually ends in -arm64 rather than -amd64. Confirm the path with readlink -f "$(command -v javac)" before exporting it. For a full walkthrough that covers Windows, macOS, and Linux, including JAVA_HOME configuration and switching between JDK versions, see this guide to installing JDK 8 locally to reproduce the build environment.
You can also debug against the same container image without copying the toolchain to the host:
docker run --rm -it \ -v "$PWD":/workspace \ -w /workspace \ legacy-jdk8-ci:local Inside the container, run mvn -version and compare it with the host output. The JDK vendor, version, and Maven version should match. If they do not, fix the image or the local install before debugging the application.
When behavior still differs, check the environment rather than the compiler. The Temurin image sets LANG and LC_ALL to en_US.UTF-8, so compare locale output if the build is encoding-sensitive. Also compare MAVEN_OPTS, timezone, any mounted settings.xml, and file ownership on the workspace. Those differences are common causes of "works in the container, fails on my laptop" puzzles.
Jenkins agents: keep the agent JVM separate
A JDK 8 build image is not automatically a valid Jenkins agent image. Jenkins' upgrade guide for Java 11 explains that agents must run at least the minimum Java version required by the controller. Jenkins 2.361.1 LTS requires Java 11 or 17 for the controller and agents. Keep the agent process on that supported JVM even when the job compiles with JDK 8.
The job toolchain is a different setting. Jenkins can launch an agent with Java 11 or 17 while a build step uses JDK 8, as long as the agent process itself stays on the supported JVM. That is the distinction behind most "Jenkins no longer supports JDK 8" migration questions.
| Approach | How it works | Best for |
|---|---|---|
| Docker pipeline agent | agent { docker { image ... } } runs the stage in the JDK 8 container while the Jenkins agent JVM stays outside it | Most Jenkinsfile pipelines |
| Kubernetes pod | A jnlp container runs Java 11 or 17; a separate jdk8 container runs the build | Kubernetes plugin users |
| Single image with two JDKs | Keep Java 11 or 17 as the default for remoting; expose JDK 8 at a separate path and set it only in build steps | Constrained environments |
A declarative pipeline can use the pinned image as a stage-level build environment:
pipeline { agent any stages { stage('Build on JDK 8') { agent { docker { image 'registry.example.com/legacy-jdk8-ci@sha256:<image-digest>' args '-u 10001:10001' } } steps { sh 'java -version && mvn -B -ntp verify' } } } } If you use the Docker plugin's "Connect with SSH" mode, the SSH-launched agent process runs with the image's Java unless you override the agent's Java path. A JDK 8-only image will fail to launch that agent on Jenkins 2.361.1 and later. Point the agent template at a JDK 11 or 17 image and run the JDK 8 image as a build container, or keep a separate supported JDK in the image for the agent process. Do not set JAVA_HOME to JDK 8 for the agent itself just to make the job compile; that breaks the remoting layer.
Conclusion
A Docker image with JDK 8 for a legacy CI build does not have to be an abandoned image. Use Eclipse Temurin 8, pin the base by digest, set JAVA_HOME and PATH explicitly, run the build as a non-root user, and verify java, javac, and the real Maven or Gradle command inside the image. Keep the Jenkins agent JVM on a supported Java version and use the JDK 8 image only as the build environment. Finally, schedule deliberate digest and Maven updates, and reproduce the same toolchain locally when you need to debug. That gives legacy builds the stability they need without relying on stale, mutable tags.
原文:https://www.a8q9.cn/en/article/docker-image-with-jdk-8-for-legacy-ci-builds-65383f.html