← All recipes
Open source

Change Docker FROM

Recipe IDorg.openrewrite.docker.ChangeFromArtifactorg.openrewrite:rewrite-docker

Change the base image in a Dockerfile FROM instruction. Each * in an old* glob is a positional capture; $N in the paired new* substitutes capture N. $0 substitutes the full original value; \$ is a literal dollar.

Single recipeApache 2.0

Add to your build

Get a download tokenSign in with GitHub or Google, and your token replaces YOUR_DOWNLOAD_TOKEN below. Free for open source recipes like this one.

~/.gradle/gradle.properties

codeGenomeUsername=you@example.com
codeGenomeToken=YOUR_DOWNLOAD_TOKEN

build.gradle.kts

plugins {
    id("org.openrewrite.rewrite") version("latest.release")
}

rewrite {
    activeRecipe("org.openrewrite.docker.ChangeFrom")
}

repositories {
    mavenCentral()
    maven {
        url = uri("https://artifacts.codegenomeproject.org/maven")
        credentials {
            username = providers.gradleProperty("codeGenomeUsername").get()
            password = providers.gradleProperty("codeGenomeToken").get()
        }
    }
}

dependencies {
    rewrite("org.openrewrite:rewrite-docker:8.92.8")
}

Then run

./gradlew rewriteRun

This recipe has required options, which a build sets in a rewrite.yml recipe that you activate instead.

Usage

You’ll need the Moderne CLI configured before running the command below.

mod run . --recipe org.openrewrite.docker.ChangeFrom --recipe-option "oldImageName=ubuntu"

If the recipe isn’t available locally, install it with:

mod config recipes jar install org.openrewrite:rewrite-docker:RELEASE

Options

NameTypeDescription
oldImageNamerequiredStringGlob pattern to match image names (without tag/digest).
e.g. ubuntu
oldTagStringOnly match images with tags matching this glob pattern. If null, matches any tag or no tag.
e.g. 20.*
oldDigestStringOnly match images with digests matching this glob pattern. If null, matches any digest or no digest. If empty (""), matches only images that have no digest, which is useful for skipping digest-pinned FROMs so deliberate pins are left untouched.
e.g. sha256:*
oldPlatformStringOnly change images with this platform. If null, matches any platform.
e.g. linux/amd64
newImageNameStringThe new image name. If null, preserves the existing name.
e.g. ubuntu
newTagStringThe new tag. If null, preserves the existing tag. If empty, removes the tag.
e.g. 22.04
newDigestStringThe new digest. If null, preserves the existing digest. If empty, removes the digest.
e.g. sha256:abc123...
newPlatformStringThe new platform. If null, preserves the existing platform. If empty, removes the platform flag.
e.g. linux/arm64

Data tables

Structured output this recipe can produce.

  • Source files that had resultsSource files that were modified by the recipe run.org.openrewrite.table.SourcesFileResults
  • Source files that had search resultsSearch results that were found during the recipe run.org.openrewrite.table.SearchResults
  • Source files that errored on a recipeThe details of all errors produced by a recipe run.org.openrewrite.table.SourcesFileErrors
  • Recipe performanceStatistics used in analyzing the performance of recipes.org.openrewrite.table.RecipeRunStats