Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buildx cache-from / cache-to #1621

Closed
owetterau opened this issue Nov 25, 2022 · 8 comments · Fixed by #1730
Closed

buildx cache-from / cache-to #1621

owetterau opened this issue Nov 25, 2022 · 8 comments · Fixed by #1730
Assignees

Comments

@owetterau
Copy link

Hello,

I am wondering how to set 'cache-from' and 'cache-to' when using buildx to build multi-platform images.

I have a maven project in which several Docker images are being built and some of them depend on each other. Unfortunately, when a depending Docker image is being built, Docker tries to download the base Docker image (as defined by 'FROM' in the Dockerfile) instead of reusing the already built one. This obviously fails.

I have already tested if using 'dockerStateDir' helps, but it did not work either.

Because of this I would like to use the 'cache-from' and 'cache-to' option of buildx but do not know how to do this with the docker-maven-plugin (and if it is possible at all).

Does anybody have an idea how to solve this issue?

Info

  • docker-maven-plugin version : 0.40.2
  • Maven version (mvn -v) : 3.8.1
  • Docker version : 20.10.21
@rhuss
Copy link
Collaborator

rhuss commented Feb 14, 2023

I'm not directly connected to the buildx implementation, but it sounds like a useful addition. @chonton maybe something that could be considered in the future ? Would this make sense ?

@chonton
Copy link
Contributor

chonton commented Feb 14, 2023

Maybe. The problem is that you get one export location per build - either the local filesystem or the registry. If we build the base image into the local filesystem, we can then build the dependent image and push that into registry. However, the base image will not be available in the registry. So you have to introduce another build that basically is a FROM base image and pushes to registry.

I wonder if a multi-stage build might work out. If the base stages are identical in the two dependents, then buildx is smart enough to use the cached layers. It is a build, but it is a fast build.

I also wonder if the problem is a side effect of how the build/push mojos are working. The build mojo builds the native image into the cache, not into the registry. The push mojo builds both native platform (fast build due to cache) and non-native platforms and pushes all platforms to the registry.

@owetterau Please comment on the docker cli commands you think would work for your use case.

@orange-buffalo
Copy link
Contributor

We would be interested in this feature.

Our builds already leverage layered Jars for our Spring Boot services, but lack of support for cache-from / cache-to causes our CI builds to always create full layers from scratch. As soon as we can use buildX caches, the layered Jars start bringing benefits of smaller images.

From what I can see, it should be straightforward to pass the config from Maven mojo into buildX command line arguments:

append(cmdLine, "build", "--progress=plain", "--builder", builderName, "--platform",
String.join(",", platforms), "--tag",
new ImageName(imageConfig.getName()).getFullName(configuredRegistry));

Will maintainers accept the minimalist PR to pass the values through?

@rohanKanojia
Copy link
Member

rohanKanojia commented Nov 29, 2023

@orange-buffalo : Do you think #1717 is related to this issue?

Do you propose to pass already existing property cacheFrom to buildx arguments? How would we configure cacheTo?

@orange-buffalo
Copy link
Contributor

Yes, this PR is related. It provides existing configuration option into buildX --no-cache arg. My suggestion is to implement similar functionality for --cache-from and --cache-to args, adding new config to the plugin.

To me it sounds logical to include this configuration into buildX of image.build:

<configuration>
  <images>
    <image>
      <name>my-registry/my-image:${project.version}</name>
      <build>
        <buildx>
          <!-- already supported properties -->
          <builderName>my_name</builderName>
          <platforms>
            <platform>linux/amd64,linux/arm64</platform>
          </platforms>
          <!-- new properties -->
          <cacheFrom>my-registry/my-image-cache:latest</cacheFrom>
          <cacheTo>my-registry/my-image-cache:latest</cacheTo>
        </buildx>

And also add support to override them with system properties, like in the referenced PR:

public static boolean isNoCache(ImageConfiguration imageConfig) {
String noCache = System.getProperty("docker.noCache");
if (noCache == null) {
noCache = System.getProperty("docker.nocache");
}

@rohanKanojia
Copy link
Member

rohanKanojia commented Nov 29, 2023

@owetterau : Sounds good to me (however, I would prefer these properties as maven mojo parameters rather than system properties), Shall I assign this issue to you?

@orange-buffalo
Copy link
Contributor

Sure, let's do that. I expect to provide a PR during the next week or two.

@rohanKanojia
Copy link
Member

@orange-buffalo : Thanks a lot!

orange-buffalo added a commit to orange-buffalo/fabric8io-docker-maven-plugin that referenced this issue Nov 30, 2023
Closes fabric8io#1621

Signed-off-by: Bogdan Ilchyshyn <orange-buffalo@users.noreply.github.com>
rohanKanojia pushed a commit that referenced this issue Dec 3, 2023
Closes #1621

Signed-off-by: Bogdan Ilchyshyn <orange-buffalo@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants