Skip to content

buildx cache-from / cache-to #1621

Closed

Description

@owetterau

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

Activity

  1. rhuss commented on Feb 14, 2023

    @rhuss
    Collaborator

    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 ?

  2. chonton commented on Feb 14, 2023

    @chonton
    Contributor

    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.

  3. orange-buffalo commented on Nov 29, 2023

    @orange-buffalo
    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?

  4. rohanKanojia commented on Nov 29, 2023

    @rohanKanojia
    Member

    @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?

  5. orange-buffalo commented on Nov 29, 2023

    @orange-buffalo
    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");
    }

  6. rohanKanojia commented on Nov 29, 2023

    @rohanKanojia
    Member

    @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?

  7. orange-buffalo commented on Nov 29, 2023

    @orange-buffalo
    Contributor

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

  8. rohanKanojia commented on Nov 29, 2023

    @rohanKanojia
    Member

    @orange-buffalo : Thanks a lot!

  9. added a commit that references this issue on Nov 30, 2023
    30de2d6
  10. added a commit that references this issue on Dec 3, 2023
    c5cc4db
  11. ggjulio commented on Sep 20, 2024

    @ggjulio

    Having two cacheFrom is a common pattern but currently not supported by the plugin.
    I opened an issue #1822

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions