From b0cbab5183170ddc4aa43e37d70a1d48e00425e5 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Mon, 27 Jul 2020 08:34:55 +0300 Subject: [PATCH] Add support for cache-from in Docker extension Fixes: #10966 --- .../image/docker/deployment/DockerConfig.java | 7 +++++++ .../image/docker/deployment/DockerProcessor.java | 15 +++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/extensions/container-image/container-image-docker/deployment/src/main/java/io/quarkus/container/image/docker/deployment/DockerConfig.java b/extensions/container-image/container-image-docker/deployment/src/main/java/io/quarkus/container/image/docker/deployment/DockerConfig.java index db9c9b3dcbe68..7a7bedbce1200 100644 --- a/extensions/container-image/container-image-docker/deployment/src/main/java/io/quarkus/container/image/docker/deployment/DockerConfig.java +++ b/extensions/container-image/container-image-docker/deployment/src/main/java/io/quarkus/container/image/docker/deployment/DockerConfig.java @@ -1,5 +1,6 @@ package io.quarkus.container.image.docker.deployment; +import java.util.List; import java.util.Map; import java.util.Optional; @@ -33,4 +34,10 @@ public class DockerConfig { */ @ConfigItem public Map buildArgs; + + /** + * Images to consider as cache sources. Values are passed to {@code docker build} via the {@code cache-from} option + */ + @ConfigItem + public Optional> cacheFrom; } diff --git a/extensions/container-image/container-image-docker/deployment/src/main/java/io/quarkus/container/image/docker/deployment/DockerProcessor.java b/extensions/container-image/container-image-docker/deployment/src/main/java/io/quarkus/container/image/docker/deployment/DockerProcessor.java index dc4c84ab46aa0..54277ac78d738 100644 --- a/extensions/container-image/container-image-docker/deployment/src/main/java/io/quarkus/container/image/docker/deployment/DockerProcessor.java +++ b/extensions/container-image/container-image-docker/deployment/src/main/java/io/quarkus/container/image/docker/deployment/DockerProcessor.java @@ -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); @@ -170,12 +170,19 @@ private void createContainerImage(ContainerImageConfig containerImageConfig, Doc } } - private String[] getDockerArgs(String image, DockerfilePaths dockerfilePaths, Map buildArgs) { - List dockerArgs = new ArrayList<>(6 + buildArgs.size()); + private String[] getDockerArgs(String image, DockerfilePaths dockerfilePaths, DockerConfig dockerConfig) { + List dockerArgs = new ArrayList<>(6 + dockerConfig.buildArgs.size()); dockerArgs.addAll(Arrays.asList("build", "-f", dockerfilePaths.getDockerfilePath().toAbsolutePath().toString())); - for (Map.Entry entry : buildArgs.entrySet()) { + for (Map.Entry entry : dockerConfig.buildArgs.entrySet()) { dockerArgs.addAll(Arrays.asList("--build-arg", entry.getKey() + "=" + entry.getValue())); } + if (dockerConfig.cacheFrom.isPresent()) { + List 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]);