Skip to content

Do not rewrite Docker FROM images built from variables - #1212

Merged
timtebeek merged 2 commits into
mainfrom
tim/worcester-v2
Aug 20, 2026
Merged

Do not rewrite Docker FROM images built from variables#1212
timtebeek merged 2 commits into
mainfrom
tim/worcester-v2

Conversation

@timtebeek

@timtebeek timtebeek commented Aug 20, 2026

Copy link
Copy Markdown
Member

Fixes #1211

What's changed

UpgradeDockerImageVersion built a ChangeFrom sub-recipe for every (image, oldVersion) pair — 170 of them when targeting Java 25.

DockerFrom's matcher substitutes a * for each environment variable and then matches bidirectionally, so for

ARG IMAGE_NAME
ARG IMAGE_TAG
FROM ${IMAGE_NAME}:${IMAGE_TAG}

the image name matched as matchesGlob("openjdk", "*") and the tag as matchesGlob("8*", "*"). The first ChangeFrom in the list therefore fired, the $1 capture resolved to nothing, and the reference was replaced with a hardcoded eclipse-temurin:25.

This replaces the sub-recipe list with a single visitor that:

  • skips any FROM whose image name or tag contains a $, since the value can not be determined statically;
  • parses the leading version from the tag and only bumps it when it sits between 8 and the target version, preserving the suffix (-jre-alpine, -jdk-jammy, ...);
  • maps openjdk/adoptopenjdk to eclipse-temurin;
  • drops any digest pin when it bumps the tag. The digest, not the tag, decides which image is pulled, so eclipse-temurin:25-jre@sha256:<openjdk-11-digest> would keep resolving to the pre-upgrade image.

As a side effect the recipe list of the aggregate migrations shrinks considerably: UpgradeToJava25 drops from 1398 to 488 recipes.

Tests

Added cases covering ${VAR} and $VAR in the image name, the tag, and both; a Dockerfile that mixes a variable FROM with a literal one; unrelated images; and digest pin removal. The existing parameterized cases are unchanged and still pass.

`UpgradeDockerImageVersion` composed a `ChangeFrom` per (image, oldVersion)
pair. `DockerFrom`'s matcher treats an environment variable as a `*` wildcard
and matches bidirectionally, so `FROM ${IMAGE_NAME}:${IMAGE_TAG}` matched every
one of those globs and was rewritten to a hardcoded `eclipse-temurin:25`.

Replace the ~170 sub-recipes with a single visitor that reads the image name and
tag, skips any reference containing a variable, and bumps only tags whose
leading version is between 8 and the target version.
The digest, not the tag, decides which image is pulled, so carrying the old
digest over would keep resolving to the pre-upgrade image.
@timtebeek
timtebeek marked this pull request as ready for review August 20, 2026 13:43
@timtebeek
timtebeek merged commit 8b1f865 into main Aug 20, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/worcester-v2 branch August 20, 2026 13:44
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

UpgradeDockerImageVersion hardcoded Dockerfile image issue

1 participant