diff --git a/doc/changelog.md b/doc/changelog.md index bb596d92c..40fbf5966 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -3,6 +3,7 @@ - Automatically create parent directories of portPropertyFile path - Added support for `platform` attribute of a container in the docker-compose configuration. - `docker:push` failed with build `ARG` in `FROM` ([1778](https://github.com/fabric8io/docker-maven-plugin/issues/1778)) + - `FROM` can reference `ARG` that references other `ARG` ([1800](https://github.com/fabric8io/docker-maven-plugin/issues/1800)) * **0.44.0** (2024-02-17): - Add new option "useDefaultExclusion" for build configuration to handle exclusion of hidden files ([1708](https://github.com/fabric8io/docker-maven-plugin/issues/1708)) diff --git a/src/main/java/io/fabric8/maven/docker/util/DockerFileUtil.java b/src/main/java/io/fabric8/maven/docker/util/DockerFileUtil.java index fef69193e..2caf40cc7 100644 --- a/src/main/java/io/fabric8/maven/docker/util/DockerFileUtil.java +++ b/src/main/java/io/fabric8/maven/docker/util/DockerFileUtil.java @@ -185,9 +185,12 @@ static String resolveImageTagFromArgs(String imageTagString, Map String resolvedImageString = imageTagString; Set foundArgs = findAllArgs(imageTagString); for (String foundArg : foundArgs) { + // check if the arg referenced in imageTagString exists in the Dockerfile if (args.containsKey(foundArg)) { - resolvedImageString = resolvedImageString.replaceFirst(String.format("\\$\\{*%s\\}*", foundArg), - args.get(foundArg)); + // resolve args that are referenced by this arg + String arg = resolveImageTagFromArgs(args.get(foundArg), args); + // replace the arg by its value + resolvedImageString = resolvedImageString.replaceFirst(String.format("\\$\\{*%s\\}*", foundArg), arg); } } return resolvedImageString; diff --git a/src/test/java/io/fabric8/maven/docker/util/DockerFileUtilTest.java b/src/test/java/io/fabric8/maven/docker/util/DockerFileUtilTest.java index 79daacf5c..0d79da99f 100644 --- a/src/test/java/io/fabric8/maven/docker/util/DockerFileUtilTest.java +++ b/src/test/java/io/fabric8/maven/docker/util/DockerFileUtilTest.java @@ -52,6 +52,35 @@ void testSimple() throws Exception { toTest, FixedStringSearchInterpolator.create(), Collections.emptyMap()).get(0)); } + @Test + void testSimpleWithArgs() throws Exception { + File toTest = copyToTempDir("Dockerfile_from_simple_with_args"); + // default arg value + Assertions.assertEquals("alpine:latest", DockerFileUtil.extractBaseImages( + toTest, FixedStringSearchInterpolator.create(), Collections.emptyMap()).get(0)); + // given arg value + Assertions.assertEquals("alpine:3", DockerFileUtil.extractBaseImages( + toTest, FixedStringSearchInterpolator.create(), Collections.singletonMap("VERSION", "3")).get(0)); + } + + @Test + void testSimpleWithChainedArgs() throws Exception { + File toTest = copyToTempDir("Dockerfile_from_simple_with_chained_args"); + // default arg value + Assertions.assertEquals("alpine:latest", DockerFileUtil.extractBaseImages( + toTest, FixedStringSearchInterpolator.create(), Collections.emptyMap()).get(0)); + // given arg value + Assertions.assertEquals("archlinux:latest", DockerFileUtil.extractBaseImages( + toTest, FixedStringSearchInterpolator.create(), Collections.singletonMap("IMAGE_NAME", "archlinux")).get(0)); + Assertions.assertEquals("alpine:3", DockerFileUtil.extractBaseImages( + toTest, FixedStringSearchInterpolator.create(), Collections.singletonMap("IMAGE_TAG", "3")).get(0)); + Map buildArgsFromConfig = new HashMap<>(); + buildArgsFromConfig.put("IMAGE_NAME", "archlinux"); + buildArgsFromConfig.put("IMAGE_TAG", "base-devel"); + Assertions.assertEquals("archlinux:base-devel", DockerFileUtil.extractBaseImages( + toTest, FixedStringSearchInterpolator.create(), buildArgsFromConfig).get(0)); + } + @Test void testMultiStage() throws Exception { File toTest = copyToTempDir("Dockerfile_multi_stage"); diff --git a/src/test/resources/io/fabric8/maven/docker/util/Dockerfile_from_simple_with_args b/src/test/resources/io/fabric8/maven/docker/util/Dockerfile_from_simple_with_args new file mode 100644 index 000000000..afe0b09b1 --- /dev/null +++ b/src/test/resources/io/fabric8/maven/docker/util/Dockerfile_from_simple_with_args @@ -0,0 +1,3 @@ +# Dockerfile with a simple FROM clause with ARG +ARG VERSION=latest +FROM alpine:${VERSION} diff --git a/src/test/resources/io/fabric8/maven/docker/util/Dockerfile_from_simple_with_chained_args b/src/test/resources/io/fabric8/maven/docker/util/Dockerfile_from_simple_with_chained_args new file mode 100644 index 000000000..4f465e862 --- /dev/null +++ b/src/test/resources/io/fabric8/maven/docker/util/Dockerfile_from_simple_with_chained_args @@ -0,0 +1,5 @@ +# Dockerfile with a simple FROM clause with ARGs +ARG IMAGE_NAME=alpine +ARG IMAGE_TAG=latest +ARG FULL_IMAGE=${IMAGE_NAME}:${IMAGE_TAG} +FROM ${FULL_IMAGE}