From 452123d1385f670950c5edb86acf9af088e99b78 Mon Sep 17 00:00:00 2001 From: Sergiu Dumitriu Date: Tue, 5 Dec 2023 20:17:29 -0500 Subject: [PATCH] Add support for using maven/system properties for an ARG used as the FROM image in a dockerfile --- it/dockerfile-base-as-arg/README.md | 13 +++ it/dockerfile-base-as-arg/pom.xml | 100 ++++++++++++++++++ .../src/main/docker/Dockerfile | 16 +++ .../src/main/docker/welcome.txt | 1 + .../samples/dockerfile/HelloWorldServlet.java | 26 +++++ .../src/main/webapp/WEB-INF/web.xml | 17 +++ it/pom.xml | 1 + .../maven/docker/service/BuildService.java | 15 +-- 8 files changed, 182 insertions(+), 7 deletions(-) create mode 100644 it/dockerfile-base-as-arg/README.md create mode 100644 it/dockerfile-base-as-arg/pom.xml create mode 100644 it/dockerfile-base-as-arg/src/main/docker/Dockerfile create mode 100644 it/dockerfile-base-as-arg/src/main/docker/welcome.txt create mode 100644 it/dockerfile-base-as-arg/src/main/java/io/fabric8/dmp/samples/dockerfile/HelloWorldServlet.java create mode 100644 it/dockerfile-base-as-arg/src/main/webapp/WEB-INF/web.xml diff --git a/it/dockerfile-base-as-arg/README.md b/it/dockerfile-base-as-arg/README.md new file mode 100644 index 000000000..e15316ad4 --- /dev/null +++ b/it/dockerfile-base-as-arg/README.md @@ -0,0 +1,13 @@ +## d-m-p sample using a Dockerfile + +This example shows how to use docker-maven-plugin together with a Dockerfile. +It is a simple `HelloWorld` servlet running on top of Jetty at the root context. + +The [Dockerfile](src/main/docker/Dockerfile) is located is `src/main/docker`. +Please note how the assembly is added using the directory `maven` which will be created on the fly by this plugin when an `` is specified. + +To build and start a Jetty container with a mapped port at 8080 on the Docker host use + +``` +mvn package docker:build docker:run +``` \ No newline at end of file diff --git a/it/dockerfile-base-as-arg/pom.xml b/it/dockerfile-base-as-arg/pom.xml new file mode 100644 index 000000000..fbab95c74 --- /dev/null +++ b/it/dockerfile-base-as-arg/pom.xml @@ -0,0 +1,100 @@ + + + + + + 4.0.0 + + + io.fabric8.dmp.itests + dmp-it-parent + 0.44-SNAPSHOT + ../pom.xml + + + dmp-it-dockerfile-base-as-arg + 0.44-SNAPSHOT + war + dmp-it-dockerfile + + + jetty + welcome.txt + ${project.artifactId} + + + + + commons-io + commons-io + 2.7 + + + javax.servlet + javax.servlet-api + 3.1.0 + + + junit + junit + 4.13.1 + test + + + + + + + io.fabric8 + docker-maven-plugin + + + + fabric8:dmp-it-dockerfile + dockerfile + + + ${project.basedir}/src/main/docker + + rootWar + + + + + 8080:8080 + + + + + + + + build + + build + + install + + + start + + start + + install + + + stop + + stop + + install + + + + + + diff --git a/it/dockerfile-base-as-arg/src/main/docker/Dockerfile b/it/dockerfile-base-as-arg/src/main/docker/Dockerfile new file mode 100644 index 000000000..dbbd86192 --- /dev/null +++ b/it/dockerfile-base-as-arg/src/main/docker/Dockerfile @@ -0,0 +1,16 @@ +# Sample Dockerfile for use with the Docker file mode +ARG baseImage +FROM ${baseImage} + +ENV SAMPLE_BUILD_MODE=dockerfile +LABEL PROJECT_NAME=hello-world \ + PROJECT=${project.artifactId} + +# Arbitrary files can be added +ADD ${file} / + +# In maven/ the files as specified in the section is stored +# and need to be added manually +COPY maven/ /var/lib/jetty/webapps/ + +EXPOSE 8080 diff --git a/it/dockerfile-base-as-arg/src/main/docker/welcome.txt b/it/dockerfile-base-as-arg/src/main/docker/welcome.txt new file mode 100644 index 000000000..5902a5714 --- /dev/null +++ b/it/dockerfile-base-as-arg/src/main/docker/welcome.txt @@ -0,0 +1 @@ +Hello World !!! \ No newline at end of file diff --git a/it/dockerfile-base-as-arg/src/main/java/io/fabric8/dmp/samples/dockerfile/HelloWorldServlet.java b/it/dockerfile-base-as-arg/src/main/java/io/fabric8/dmp/samples/dockerfile/HelloWorldServlet.java new file mode 100644 index 000000000..e8eddae95 --- /dev/null +++ b/it/dockerfile-base-as-arg/src/main/java/io/fabric8/dmp/samples/dockerfile/HelloWorldServlet.java @@ -0,0 +1,26 @@ +package io.fabric8.dmp.itests.dockerfile; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.io.FileUtils; + +/** + * @author roland + * @since 18.04.17 + */ +public class HelloWorldServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String txt = FileUtils.readFileToString(new File("/welcome.txt"), Charset.defaultCharset()); + resp.getWriter().append(txt).flush(); + resp.setHeader("Content-Type", "plain/text"); + } +} diff --git a/it/dockerfile-base-as-arg/src/main/webapp/WEB-INF/web.xml b/it/dockerfile-base-as-arg/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 000000000..17b078d0d --- /dev/null +++ b/it/dockerfile-base-as-arg/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,17 @@ + + + + + HelloWold + hello-world + io.fabric8.dmp.itests.dockerfile.HelloWorldServlet + + + + hello-world + / + + + \ No newline at end of file diff --git a/it/pom.xml b/it/pom.xml index b104d0cc6..5b9c694f6 100644 --- a/it/pom.xml +++ b/it/pom.xml @@ -30,6 +30,7 @@ docker-compose docker-compose-dependon dockerfile + dockerfile-base-as-arg dockerignore healthcheck helloworld diff --git a/src/main/java/io/fabric8/maven/docker/service/BuildService.java b/src/main/java/io/fabric8/maven/docker/service/BuildService.java index 3958eb5c7..b694bdb8b 100644 --- a/src/main/java/io/fabric8/maven/docker/service/BuildService.java +++ b/src/main/java/io/fabric8/maven/docker/service/BuildService.java @@ -64,12 +64,13 @@ public class BuildService { public void buildImage(ImageConfiguration imageConfig, ImagePullManager imagePullManager, BuildContext buildContext, File buildArchiveFile) throws DockerAccessException, MojoExecutionException { + Map buildArgs = addBuildArgs(buildContext); if (imagePullManager != null) { - autoPullBaseImage(imageConfig, imagePullManager, buildContext); + autoPullBaseImage(imageConfig, imagePullManager, buildContext, buildArgs); autoPullCacheFromImage(imageConfig, imagePullManager, buildContext); } - buildImage(imageConfig, buildContext.getMojoParameters(), ConfigHelper.isNoCache(imageConfig), checkForSquash(imageConfig), addBuildArgs(buildContext), buildArchiveFile); + buildImage(imageConfig, buildContext.getMojoParameters(), ConfigHelper.isNoCache(imageConfig), checkForSquash(imageConfig), buildArgs, buildArchiveFile); } /** @@ -357,7 +358,7 @@ private Map addBuildArgsFromDockerConfig() { return buildArgs; } - private void autoPullBaseImage(ImageConfiguration imageConfig, ImagePullManager imagePullManager, BuildContext buildContext) + private void autoPullBaseImage(ImageConfiguration imageConfig, ImagePullManager imagePullManager, BuildContext buildContext, Map buildArgs) throws DockerAccessException, MojoExecutionException { BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration(); CleanupMode cleanupMode = buildConfig.cleanupMode(); @@ -369,12 +370,12 @@ private void autoPullBaseImage(ImageConfiguration imageConfig, ImagePullManager List fromImages; if (buildConfig.isDockerFileMode()) { - fromImages = extractBaseFromDockerfile(buildConfig, buildContext); + fromImages = extractBaseFromDockerfile(buildConfig, buildContext, buildArgs); } else { fromImages = new LinkedList<>(); String baseImage = extractBaseFromConfiguration(buildConfig); if (baseImage != null) { - fromImages.add(extractBaseFromConfiguration(buildConfig)); + fromImages.add(baseImage); } } for (String fromImage : fromImages) { @@ -431,14 +432,14 @@ private String extractBaseFromConfiguration(BuildImageConfiguration buildConfig) return fromImage; } - private List extractBaseFromDockerfile(BuildImageConfiguration buildConfig, BuildContext buildContext) { + private List extractBaseFromDockerfile(BuildImageConfiguration buildConfig, BuildContext buildContext, Map buildArgs) { List fromImage; try { File fullDockerFilePath = buildConfig.getAbsoluteDockerFilePath(buildContext.getMojoParameters()); fromImage = DockerFileUtil.extractBaseImages( fullDockerFilePath, DockerFileUtil.createInterpolator(buildContext.getMojoParameters(), buildConfig.getFilter()), - buildConfig.getArgs()); + buildArgs); } catch (IOException e) { // Cant extract base image, so we wont try an auto pull. An error will occur later anyway when // building the image, so we are passive here.