Skip to content

Commit

Permalink
Add support for using maven/system properties for an ARG used as the …
Browse files Browse the repository at this point in the history
…FROM image in a dockerfile
  • Loading branch information
sdumitriu authored and rohanKanojia committed Dec 9, 2023
1 parent c5cc4db commit 452123d
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 7 deletions.
13 changes: 13 additions & 0 deletions it/dockerfile-base-as-arg/README.md
Original file line number Diff line number Diff line change
@@ -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 `<assembly>` 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
```
100 changes: 100 additions & 0 deletions it/dockerfile-base-as-arg/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<!--
Simple sample program including Java code.
This helloworld exactly has been taken over mostly from https://github.com/arun-gupta/docker-java-sample.git
-->

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.fabric8.dmp.itests</groupId>
<artifactId>dmp-it-parent</artifactId>
<version>0.44-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>dmp-it-dockerfile-base-as-arg</artifactId>
<version>0.44-SNAPSHOT</version>
<packaging>war</packaging>
<name>dmp-it-dockerfile</name>

<properties>
<docker.buildArg.baseImage>jetty</docker.buildArg.baseImage>
<file>welcome.txt</file>
<project.artifactId>${project.artifactId}</project.artifactId>
</properties>

<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<images>
<image>
<name>fabric8:dmp-it-dockerfile</name>
<alias>dockerfile</alias>
<build>
<!-- filter>@</filter-->
<contextDir>${project.basedir}/src/main/docker</contextDir>
<assembly>
<descriptorRef>rootWar</descriptorRef>
</assembly>
</build>
<run>
<ports>
<port>8080:8080</port>
</ports>
</run>
</image>
</images>
</configuration>
<executions>
<execution>
<id>build</id>
<goals>
<goal>build</goal>
</goals>
<phase>install</phase>
</execution>
<execution>
<id>start</id>
<goals>
<goal>start</goal>
</goals>
<phase>install</phase>
</execution>
<execution>
<id>stop</id>
<goals>
<goal>stop</goal>
</goals>
<phase>install</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
16 changes: 16 additions & 0 deletions it/dockerfile-base-as-arg/src/main/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -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 <assembly> section is stored
# and need to be added manually
COPY maven/ /var/lib/jetty/webapps/

EXPOSE 8080
1 change: 1 addition & 0 deletions it/dockerfile-base-as-arg/src/main/docker/welcome.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World !!!
Original file line number Diff line number Diff line change
@@ -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");
}
}
17 changes: 17 additions & 0 deletions it/dockerfile-base-as-arg/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<servlet>
<display-name>HelloWold</display-name>
<servlet-name>hello-world</servlet-name>
<servlet-class>io.fabric8.dmp.itests.dockerfile.HelloWorldServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>hello-world</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>
1 change: 1 addition & 0 deletions it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<module>docker-compose</module>
<module>docker-compose-dependon</module>
<module>dockerfile</module>
<module>dockerfile-base-as-arg</module>
<module>dockerignore</module>
<module>healthcheck</module>
<module>helloworld</module>
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/io/fabric8/maven/docker/service/BuildService.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ public class BuildService {
public void buildImage(ImageConfiguration imageConfig, ImagePullManager imagePullManager, BuildContext buildContext, File buildArchiveFile)
throws DockerAccessException, MojoExecutionException {

Map<String, String> 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);
}

/**
Expand Down Expand Up @@ -357,7 +358,7 @@ private Map<String, String> addBuildArgsFromDockerConfig() {
return buildArgs;
}

private void autoPullBaseImage(ImageConfiguration imageConfig, ImagePullManager imagePullManager, BuildContext buildContext)
private void autoPullBaseImage(ImageConfiguration imageConfig, ImagePullManager imagePullManager, BuildContext buildContext, Map<String, String> buildArgs)
throws DockerAccessException, MojoExecutionException {
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
CleanupMode cleanupMode = buildConfig.cleanupMode();
Expand All @@ -369,12 +370,12 @@ private void autoPullBaseImage(ImageConfiguration imageConfig, ImagePullManager

List<String> 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) {
Expand Down Expand Up @@ -431,14 +432,14 @@ private String extractBaseFromConfiguration(BuildImageConfiguration buildConfig)
return fromImage;
}

private List<String> extractBaseFromDockerfile(BuildImageConfiguration buildConfig, BuildContext buildContext) {
private List<String> extractBaseFromDockerfile(BuildImageConfiguration buildConfig, BuildContext buildContext, Map<String, String> buildArgs) {
List<String> 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.
Expand Down

0 comments on commit 452123d

Please sign in to comment.