Skip to content

Commit

Permalink
Add support for Docker's --build-arg
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed May 29, 2020
1 parent 7d826f8 commit 8d5b8a7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.container.image.docker.deployment;

import java.util.Map;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigItem;
Expand All @@ -26,4 +27,10 @@ public class DockerConfig {
*/
@ConfigItem
public Optional<String> dockerfileNativePath;

/**
* Build args passed to docker via {@code --build-arg}
*/
@ConfigItem
public Map<String, String> buildArgs;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.nio.file.Paths;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -42,6 +43,7 @@ public class DockerProcessor {
private static final String DOCKER = "docker";
private static final String DOCKERFILE_JVM = "Dockerfile.jvm";
private static final String DOCKERFILE_NATIVE = "Dockerfile.native";
public static final String DOCKER_BINARY_NAME = "docker";

private final DockerWorking dockerWorking = new DockerWorking();

Expand Down Expand Up @@ -123,11 +125,12 @@ private void createContainerImage(ContainerImageConfig containerImageConfig, Doc
OutputTargetBuildItem out, ImageIdReader reader, boolean forNative, boolean pushRequested) {

DockerfilePaths dockerfilePaths = getDockerfilePaths(dockerConfig, forNative, out);
String[] buildArgs = { "build", "-f", dockerfilePaths.getDockerfilePath().toAbsolutePath().toString(), "-t", image,
dockerfilePaths.getDockerExecutionPath().toAbsolutePath().toString() };
boolean buildSuccessful = ExecUtil.exec(out.getOutputDirectory().toFile(), reader, "docker", buildArgs);
String[] dockerArgs = getDockerArgs(image, dockerfilePaths, dockerConfig.buildArgs);
log.infof("Executing the following command to build docker image: '%s %s'", DOCKER_BINARY_NAME,
String.join(" ", dockerArgs));
boolean buildSuccessful = ExecUtil.exec(out.getOutputDirectory().toFile(), reader, DOCKER_BINARY_NAME, dockerArgs);
if (!buildSuccessful) {
throw dockerException(buildArgs);
throw dockerException(dockerArgs);
}

log.infof("Built container image %s (%s)\n", image, reader.getImageId());
Expand Down Expand Up @@ -160,6 +163,17 @@ private void createContainerImage(ContainerImageConfig containerImageConfig, Doc
}
}

private String[] getDockerArgs(String image, DockerfilePaths dockerfilePaths, Map<String, String> buildArgs) {
List<String> dockerArgs = new ArrayList<>(6 + buildArgs.size());
dockerArgs.addAll(Arrays.asList("build", "-f", dockerfilePaths.getDockerfilePath().toAbsolutePath().toString()));
for (Map.Entry<String, String> entry : buildArgs.entrySet()) {
dockerArgs.addAll(Arrays.asList("--build-arg", entry.getKey() + "=" + entry.getValue()));
}
dockerArgs.addAll(Arrays.asList("-t", image));
dockerArgs.add(dockerfilePaths.getDockerExecutionPath().toAbsolutePath().toString());
return dockerArgs.toArray(new String[0]);
}

private void createAdditionalTags(String image, List<String> additionalImageTags) {
for (String additionalTag : additionalImageTags) {
String[] tagArgs = { "tag", image, additionalTag };
Expand Down

0 comments on commit 8d5b8a7

Please sign in to comment.