Skip to content

Commit

Permalink
Add support for cache-from in Docker extension
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jul 27, 2020
1 parent a081360 commit b0cbab5
Show file tree
Hide file tree
Showing 2 changed files with 18 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.List;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -33,4 +34,10 @@ public class DockerConfig {
*/
@ConfigItem
public Map<String, String> buildArgs;

/**
* Images to consider as cache sources. Values are passed to {@code docker build} via the {@code cache-from} option
*/
@ConfigItem
public Optional<List<String>> cacheFrom;
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private void createContainerImage(ContainerImageConfig containerImageConfig, Doc
PackageConfig packageConfig) {

DockerfilePaths dockerfilePaths = getDockerfilePaths(dockerConfig, forNative, packageConfig, out);
String[] dockerArgs = getDockerArgs(image, dockerfilePaths, dockerConfig.buildArgs);
String[] dockerArgs = getDockerArgs(image, dockerfilePaths, dockerConfig);
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);
Expand Down Expand Up @@ -170,12 +170,19 @@ 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());
private String[] getDockerArgs(String image, DockerfilePaths dockerfilePaths, DockerConfig dockerConfig) {
List<String> dockerArgs = new ArrayList<>(6 + dockerConfig.buildArgs.size());
dockerArgs.addAll(Arrays.asList("build", "-f", dockerfilePaths.getDockerfilePath().toAbsolutePath().toString()));
for (Map.Entry<String, String> entry : buildArgs.entrySet()) {
for (Map.Entry<String, String> entry : dockerConfig.buildArgs.entrySet()) {
dockerArgs.addAll(Arrays.asList("--build-arg", entry.getKey() + "=" + entry.getValue()));
}
if (dockerConfig.cacheFrom.isPresent()) {
List<String> cacheFrom = dockerConfig.cacheFrom.get();
if (!cacheFrom.isEmpty()) {
dockerArgs.add("--cache-from");
dockerArgs.add(String.join(",", cacheFrom));
}
}
dockerArgs.addAll(Arrays.asList("-t", image));
dockerArgs.add(dockerfilePaths.getDockerExecutionPath().toAbsolutePath().toString());
return dockerArgs.toArray(new String[0]);
Expand Down

0 comments on commit b0cbab5

Please sign in to comment.