Skip to content

Commit

Permalink
Add replacement of build args in Dockerfile FROM statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Donnerbart committed Nov 3, 2022
1 parent 29298e5 commit c28968d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.zip.GZIPOutputStream;

@Slf4j
Expand Down Expand Up @@ -182,24 +183,39 @@ protected void configure(BuildImageCmd buildImageCmd) {

private void prePullDependencyImages(Set<String> imagesToPull) {
imagesToPull.forEach(imageName -> {
String resolvedImageName = applyBuildArgsToImageName(imageName);
try {
log.info(
"Pre-emptively checking local images for '{}', referenced via a Dockerfile. If not available, it will be pulled.",
imageName
resolvedImageName
);
new RemoteDockerImage(DockerImageName.parse(imageName))
new RemoteDockerImage(DockerImageName.parse(resolvedImageName))
.withImageNameSubstitutor(ImageNameSubstitutor.noop())
.get();
} catch (Exception e) {
log.warn(
"Unable to pre-fetch an image ({}) depended upon by Dockerfile - image build will continue but may fail. Exception message was: {}",
imageName,
resolvedImageName,
e.getMessage()
);
}
});
}

/**
* See {@code filterForEnvironmentVars()} in {@link com.github.dockerjava.core.dockerfile.DockerfileStatement}.
*/
private String applyBuildArgsToImageName(String imageName) {
for (Map.Entry<String, String> entry : buildArgs.entrySet()) {
String value = Matcher.quoteReplacement(entry.getValue());
// handle: $VARIABLE case
imageName = imageName.replace("$" + entry.getKey(), value);
// handle ${VARIABLE} case
imageName = imageName.replace("${" + entry.getKey() + "}", value);
}
return imageName;
}

public ImageFromDockerfile withBuildArg(final String key, final String value) {
this.buildArgs.put(key, value);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -22,6 +24,13 @@ public class DockerfileBuildTest {

@Parameterized.Parameters
public static Object[][] parameters() {
Map<String, String> buildArgs = new HashMap<>(4);
buildArgs.put("BUILD_IMAGE", "alpine:3.16");
buildArgs.put("BASE_IMAGE", "alpine");
buildArgs.put("BASE_IMAGE_TAG", "3.12");
buildArgs.put("UNUSED", "ignored");

//noinspection deprecation
return new Object[][] {
// Dockerfile build without explicit per-file inclusion
new Object[] {
Expand All @@ -38,14 +47,22 @@ public static Object[][] parameters() {
"test4567",
new ImageFromDockerfile().withFileFromPath(".", RESOURCE_PATH).withDockerfilePath("./Dockerfile-alt"),
},
// Dockerfile build using build args
// Dockerfile build using withBuildArg()
new Object[] {
"test7890",
new ImageFromDockerfile()
.withFileFromPath(".", RESOURCE_PATH)
.withDockerfilePath("./Dockerfile-buildarg")
.withBuildArg("CUSTOM_ARG", "test7890"),
},
// Dockerfile build using withBuildArgs() with build args in FROM statement
new Object[] {
"test1234",
new ImageFromDockerfile()
.withFileFromPath(".", RESOURCE_PATH)
.withDockerfile(RESOURCE_PATH.resolve("Dockerfile-from-buildarg"))
.withBuildArgs(buildArgs),
},
// Dockerfile build using withDockerfile(File)
new Object[] {
"test4567",
Expand All @@ -62,9 +79,10 @@ public DockerfileBuildTest(String expectedFileContent, ImageFromDockerfile image
}

@Test
@SuppressWarnings("resource")
public void performTest() {
try (
final GenericContainer container = new GenericContainer(image)
final GenericContainer<?> container = new GenericContainer<>(image)
.withStartupCheckStrategy(new OneShotStartupCheckStrategy())
.withCommand("cat", "/test.txt")
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ARG BUILD_IMAGE
ARG BASE_IMAGE
ARG BASE_IMAGE_TAG

FROM ${BUILD_IMAGE} AS build
COPY localfile.txt /test-build.txt

FROM $BASE_IMAGE:${BASE_IMAGE_TAG} AS base
COPY --from=build /test-build.txt /test.txt

0 comments on commit c28968d

Please sign in to comment.