Skip to content

bake cache misses with multiple targets #414

Open

Description

@weber-software

I was hoping to speed up builds by using the parallelism provided by bake.

Therfore i'm running buildx bake -f docker-bake.hcl

group "default" {
	targets = [ "s1","s2","s3","s4","s5","s6","s7","s8","s9","s10","s11","s12","s13","s14","s15","s16","s17","s18","s19","s20","s21"]
}
target "s1" {
	context = "./s1"
}
target "s2" {
	context = "./s2"
}
target "s3" {
	context = "./s3"
}
...
target "s21" {
	context = "./s21"
}

but nearly all of the time one (or more) of the images gets rebuilt, even if they should hit the cache.

 => CACHED [s1 build-app.web 2/7] WORKDIR /app                                                                                                                                                                0.0s
 => CACHED [s1 build-app.web 3/7] COPY package*.json /app/                                                                                                                                                    0.0s
 => CACHED [s1 build-app.web 4/7] RUN npm ci                                                                                                                                                                  0.0s
 => CACHED [s1 build-app.web 5/7] COPY ./*.json /app/                                                                                                                                                         0.0s
 => CACHED [s1 build-app.web 6/7] COPY ./src /app/src                                                                                                                                                         0.0s
 => [s19 build 6/6] RUN npm run build                                                                                                                                                                         2.8s

If i specify single targets, the cache is used as i would expect:
for i in {1..21}; do buildx bake -f docker-bake.hcl s$i; done

What if found out so far:

  • The more default targets i have, the less likely the cache is used for some of them.
  • When i try to reproduce this with trivial Dockerfiles containing only a single RUN the problem doesn't occure

Are there any ideas why this is happening or how i could investigate this further?

Activity

  1. changed the title [-]bake missing cache with multiple targets[/-] [+]bake cache misses with multiple targets[/+] on Oct 15, 2020
  2. weber-software commented on Dec 21, 2020

    @weber-software
    Author

    Tested it again with 74f76cf but the problem still occures.

  3. ruffsl commented on May 2, 2024

    @ruffsl

    I'm also encountering this issue. Baking one target at a time seems to prevent any erroneous cache misses.

    At first, I had thought my issue was related to baking targets that spanned across multiple Dockerfile, and thus the multiple contexts was throwing buildkit for a loop. However, even when consolidating all my bake tarkets into a single Dockerfile, I can occasionally catch an instance where a stage that was cached for one target is somehow missed for another target, even when both targets share the same stages and context/inputs.

    For example, when baking both validator and tooler targets for the following:

    target "runner" {
      target = "runner"
    }
    
    target "prepper" {
      inherits   = ["runner"]
      target = "prepper"
    }
    
    target "validator" {
      inherits   = ["prepper"]
      target = "validator"
      tags = ["validator"]
    }
    
    target "tooler" {
      inherits   = ["validator"]
      target = "tooler"
      tags = ["tooler"]
    }

    I'll occasionally the layer for [tooler runner 4/4] miss the same layer cached for [validator runner 4/4]

    Note the difference between CACHED step #28 vs missed step #40. In particular, on step #31 the validator target starts to fetch the cache for the layer runner 3/4 , which is then followed by (another?) step #31 that starts to fetch the cache for layer runner 3/4 as well. On step #37 we can see the layer is successfully concluded as CACHED, but then later we can see step #31 finalize it's extracting. The following step #40 then subsequently misses it's cache, unline step #28 that concluded runner 4/4 as CACHED.

    I realize some of the stdout can be out of sync due to line buffering, but I think this hints at some kind of race condition when download and extracting layers that can be used in caching for multiple targets.

    @crazy-max or @tonistiigi , I can try including more telemetry if there is recommended method capturing traces, but from the log below you'll see this is using docker.io/docker/dockerfile:1.7:

    Bake Log
    [command]/usr/bin/docker buildx bake --file ./docker-bake.hcl --set *.cache-from=type=registry,ref=<URL>/<REPO>:<TAG>.cache  --provenance false --pull --push validator tooler
    #0 building with "builder-65e97194-f123-4624-b5a3-f7eedf189ce4" instance using docker-container driver
    
    #1 [internal] load local bake definitions
    #1 reading ./docker-bake.hcl 4.29kB / 4.29kB done
    #1 DONE 0.0s
    
    #2 [tooler internal] load build definition from Dockerfile
    #2 transferring dockerfile: 14.02kB done
    #2 DONE 0.1s
    
    #3 [validator internal] load build definition from Dockerfile
    #3 transferring dockerfile: 14.02kB done
    #3 DONE 0.1s
    
    #4 [tooler] resolve image config for docker-image://docker.io/docker/dockerfile:1.7
    #4 DONE 1.1s
    
    #5 [validator] docker-image://docker.io/docker/dockerfile:1.7@sha256:a57df69d0ea827fb7266491f2813635de6f17269be881f696fbfdf2d83dda33e
    #5 resolve docker.io/docker/dockerfile:1.7@sha256:a57df69d0ea827fb7266491f2813635de6f17269be881f696fbfdf2d83dda33e 0.0s done
    #5 sha256:96918c57e42509b97f10c074d80672ecdbd3bb7dcd38c1bd95960cf291207416 1.05MB / 11.98MB 0.2s
    #5 sha256:96918c57e42509b97f10c074d80672ecdbd3bb7dcd38c1bd95960cf291207416 8.39MB / 11.98MB 0.3s
    #5 sha256:96918c57e42509b97f10c074d80672ecdbd3bb7dcd38c1bd95960cf291207416 11.98MB / 11.98MB 0.4s done
    #5 extracting sha256:96918c57e42509b97f10c074d80672ecdbd3bb7dcd38c1bd95960cf291207416 0.1s done
    #5 extracting sha256:96918c57e42509b97f10c074d80672ecdbd3bb7dcd38c1bd95960cf291207416 0.1s done
    #5 DONE 0.6s
    
    #6 [validator internal] load metadata for docker.io/balenalib/intel-nuc-ubuntu:20240408
    #6 DONE 0.8s
    
    #7 [tooler internal] load .dockerignore
    #7 transferring context: 216B done
    #7 DONE 0.1s
    
    #8 [validator internal] load .dockerignore
    #8 transferring context: 216B done
    #8 DONE 0.1s
    
    #9 [tooler] importing cache manifest from s3:13787796600720870440
    #9 DONE 0.1s
    
    #10 [tooler internal] load build context
    #10 DONE 0.0s
    
    #11 [validator internal] load build context
    #11 DONE 0.0s
    
    #12 [validator balenaer 1/1] FROM docker.io/balenalib/intel-nuc-ubuntu:20240408@sha256:39a4a2434f6fbf980bd4dbdaf010f7fd952f7befb61691286b1d01447d1b460f
    #12 resolve docker.io/balenalib/intel-nuc-ubuntu:20240408@sha256:39a4a2434f6fbf980bd4dbdaf010f7fd952f7befb61691286b1d01447d1b460f 0.0s done
    #12 DONE 0.0s
    
    #13 [auth] sharing credentials for <URL>
    #13 DONE 0.0s
    
    #14 [validator] importing cache manifest from <URL>/<REPO>:<TAG>.cache
    #14 inferred cache manifest type: application/vnd.oci.image.manifest.v1+json done
    #14 inferred cache manifest type: application/vnd.oci.image.manifest.v1+json done
    #14 DONE 0.3s
    
    #10 [tooler internal] load build context
    #10 ...
    
    #11 [validator internal] load build context
    #11 transferring context: 168.30MB 3.2s done
    #11 DONE 3.3s
    
    #15 [tooler baser 1/7] RUN ...
    #15 CACHED
    
    #16 [tooler baser 3/7] RUN ...
    #16 CACHED
    
    #17 [tooler baser 4/7] RUN ...
    #17 CACHED
    
    #18 [tooler baser 6/7] RUN ...
    #18 CACHED
    
    #19 [tooler baser 2/7] RUN ...
    #19 CACHED
    
    #20 [tooler baser 5/7] WORKDIR /opt/overlay_ws
    #20 CACHED
    
    #10 [tooler internal] load build context
    #10 ...
    
    #20 [validator baser 5/7] WORKDIR /opt/overlay_ws
    #20 CACHED
    
    #19 [validator baser 2/7] RUN ...
    #19 CACHED
    
    #16 [validator baser 3/7] RUN ...
    #16 CACHED
    
    #17 [validator baser 4/7] RUN ...
    #17 CACHED
    
    #18 [validator baser 6/7] RUN ...
    #18 CACHED
    
    #15 [validator baser 1/7] RUN ...
    #15 CACHED
    
    #10 [tooler internal] load build context
    #10 transferring context: 168.30MB 3.8s done
    #10 DONE 3.9s
    
    #21 [validator baser 7/7] RUN ...
    #21 sha256:179dd77c7ae0cfd960e8ab3e38b944fe2f5ac7e69daef17cda3c54506ab8b5b1 304.02kB / 304.02kB 0.2s
    #21 sha256:179dd77c7ae0cfd960e8ab3e38b944fe2f5ac7e69daef17cda3c54506ab8b5b1 304.02kB / 304.02kB 0.2s done
    #21 sha256:6c4469e3723e128dce38cc1ef9e97dec11131acd5639a1c8ec3b8c26c05ea1fc 14.40MB / 14.40MB 0.3s done
    #21 sha256:322098fd56bbe1939f004fdd604043733f8debb2faa9e4f4de7febd625948719 120B / 120B 0.2s done
    #21 sha256:7d21316efd9c673a0d2d6aacdbf192621f81f6da8e20451958db86a0fe64165c 1.28MB / 1.28MB 0.2s done
    #21 sha256:6c4469e3723e128dce38cc1ef9e97dec11131acd5639a1c8ec3b8c26c05ea1fc 14.40MB / 14.40MB 0.3s done
    #21 sha256:180a710c6af06d06e461c7aaa1bd648fc3c158074372e7559379da88341e41f1 1.50kB / 1.50kB 0.1s done
    #21 sha256:0a6de397902022c24a65fce6ca3ded620ec131d952fd7964c23df10fb902a2a4 742B / 742B 0.1s done
    #21 sha256:8960a243af015c5f6db6a75124b5eb17db995c0debdfbb398051ad247e318691 5.24MB / 103.30MB 0.2s
    #21 sha256:01ad17f67ba5d0dc9b53520d4a06eadf0093f606e86f8d5153f1e57f51519208 487B / 487B 0.1s done
    #21 sha256:4f4fb700ef54461cfa02571ae0db9a0dc1e0cdb5577484a6d75e68dc38e8acc1 32B / 32B 0.0s done
    #21 sha256:4f4fb700ef54461cfa02571ae0db9a0dc1e0cdb5577484a6d75e68dc38e8acc1 32B / 32B 0.0s done
    #21 sha256:5204057132552536f81ec2b5d49bf5153e4c811c38ae551314b21ca506cf9ae8 443B / 443B 0.1s done
    #21 sha256:44fa9a495834bf3e73978be59e857b6f1968216c3ef0107e732e47f237260b61 12.32MB / 12.32MB 0.2s done
    #21 sha256:acd1d5dab0262fdfc24f55d6c1567d353fef3dd5a6665d30d9c4b9e80a9cf5a3 671B / 671B 0.1s done
    #21 sha256:ea9017ce1a13773d65708fa9335a96bbc9550f7205485bc080173bfb299f7f32 273B / 273B 0.1s done
    #21 sha256:8960a243af015c5f6db6a75124b5eb17db995c0debdfbb398051ad247e318691 19.92MB / 103.30MB 0.3s
    #21 sha256:44fa9a495834bf3e73978be59e857b6f1968216c3ef0107e732e47f237260b61 12.32MB / 12.32MB 0.2s done
    #21 sha256:ebdd742621cbf2a35423c5f252b6fa6d3c1fc72740928f0292471e1a874e3a07 1.01kB / 1.01kB 0.1s done
    #21 sha256:42fac631515ea6382da00e8e490f8e852624c25ee1ea51766e90cdb4d68043fd 473.57kB / 473.57kB 0.1s done
    #21 sha256:0278eb4991aa347872690d4e4f4aa89a3e44020c942397b4ad24e47dd0037dd2 5.24MB / 12.32MB 0.2s
    #21 sha256:8960a243af015c5f6db6a75124b5eb17db995c0debdfbb398051ad247e318691 51.38MB / 103.30MB 0.6s
    #21 sha256:0278eb4991aa347872690d4e4f4aa89a3e44020c942397b4ad24e47dd0037dd2 12.32MB / 12.32MB 0.2s done
    #21 sha256:bccd10f490ab0f3fba61b193d1b80af91b17ca9bdca9768a16ed05ce16552fcb 6.29MB / 29.54MB 0.2s
    #21 sha256:bccd10f490ab0f3fba61b193d1b80af91b17ca9bdca9768a16ed05ce16552fcb 29.54MB / 29.54MB 0.4s done
    #21 extracting sha256:bccd10f490ab0f3fba61b193d1b80af91b17ca9bdca9768a16ed05ce16552fcb
    #21 sha256:8960a243af015c5f6db6a75124b5eb17db995c0debdfbb398051ad247e318691 82.84MB / 103.30MB 0.9s
    #21 sha256:8960a243af015c5f6db6a75124b5eb17db995c0debdfbb398051ad247e318691 98.57MB / 103.30MB 1.1s
    #21 sha256:8960a243af015c5f6db6a75124b5eb17db995c0debdfbb398051ad247e318691 103.30MB / 103.30MB 1.2s done
    #21 sha256:8960a243af015c5f6db6a75124b5eb17db995c0debdfbb398051ad247e318691 103.30MB / 103.30MB 1.2s done
    #21 extracting sha256:bccd10f490ab0f3fba61b193d1b80af91b17ca9bdca9768a16ed05ce16552fcb 0.5s done
    #21 extracting sha256:0278eb4991aa347872690d4e4f4aa89a3e44020c942397b4ad24e47dd0037dd2
    #21 extracting sha256:0278eb4991aa347872690d4e4f4aa89a3e44020c942397b4ad24e47dd0037dd2 0.2s done
    #21 extracting sha256:0278eb4991aa347872690d4e4f4aa89a3e44020c942397b4ad24e47dd0037dd2 0.2s done
    #21 extracting sha256:42fac631515ea6382da00e8e490f8e852624c25ee1ea51766e90cdb4d68043fd 0.0s done
    #21 extracting sha256:4f4fb700ef54461cfa02571ae0db9a0dc1e0cdb5577484a6d75e68dc38e8acc1 0.0s done
    #21 extracting sha256:ebdd742621cbf2a35423c5f252b6fa6d3c1fc72740928f0292471e1a874e3a07 0.0s done
    #21 extracting sha256:ea9017ce1a13773d65708fa9335a96bbc9550f7205485bc080173bfb299f7f32 0.0s done
    #21 extracting sha256:acd1d5dab0262fdfc24f55d6c1567d353fef3dd5a6665d30d9c4b9e80a9cf5a3 0.0s done
    #21 extracting sha256:4f4fb700ef54461cfa02571ae0db9a0dc1e0cdb5577484a6d75e68dc38e8acc1 0.0s done
    #21 extracting sha256:acd1d5dab0262fdfc24f55d6c1567d353fef3dd5a6665d30d9c4b9e80a9cf5a3 0.0s done
    #21 extracting sha256:5204057132552536f81ec2b5d49bf5153e4c811c38ae551314b21ca506cf9ae8 0.0s done
    #21 extracting sha256:44fa9a495834bf3e73978be59e857b6f1968216c3ef0107e732e47f237260b61
    #21 extracting sha256:44fa9a495834bf3e73978be59e857b6f1968216c3ef0107e732e47f237260b61 0.3s done
    #21 extracting sha256:44fa9a495834bf3e73978be59e857b6f1968216c3ef0107e732e47f237260b61 0.3s done
    #21 extracting sha256:01ad17f67ba5d0dc9b53520d4a06eadf0093f606e86f8d5153f1e57f51519208 0.0s done
    #21 extracting sha256:0a6de397902022c24a65fce6ca3ded620ec131d952fd7964c23df10fb902a2a4 0.0s done
    #21 extracting sha256:180a710c6af06d06e461c7aaa1bd648fc3c158074372e7559379da88341e41f1 0.0s done
    #21 extracting sha256:8960a243af015c5f6db6a75124b5eb17db995c0debdfbb398051ad247e318691
    #21 extracting sha256:8960a243af015c5f6db6a75124b5eb17db995c0debdfbb398051ad247e318691 1.1s done
    #21 extracting sha256:8960a243af015c5f6db6a75124b5eb17db995c0debdfbb398051ad247e318691 1.1s done
    #21 extracting sha256:7d21316efd9c673a0d2d6aacdbf192621f81f6da8e20451958db86a0fe64165c 0.1s done
    #21 extracting sha256:7d21316efd9c673a0d2d6aacdbf192621f81f6da8e20451958db86a0fe64165c 0.1s done
    #21 extracting sha256:322098fd56bbe1939f004fdd604043733f8debb2faa9e4f4de7febd625948719 0.0s done
    #21 extracting sha256:6c4469e3723e128dce38cc1ef9e97dec11131acd5639a1c8ec3b8c26c05ea1fc
    #21 extracting sha256:6c4469e3723e128dce38cc1ef9e97dec11131acd5639a1c8ec3b8c26c05ea1fc 0.3s done
    #21 extracting sha256:6c4469e3723e128dce38cc1ef9e97dec11131acd5639a1c8ec3b8c26c05ea1fc 0.3s done
    #21 extracting sha256:179dd77c7ae0cfd960e8ab3e38b944fe2f5ac7e69daef17cda3c54506ab8b5b1 0.0s done
    #21 DONE 4.9s
    
    #22 [tooler cacher 1/3] COPY ...
    #22 DONE 1.2s
    
    #23 [tooler cacher 2/3] COPY ...
    #23 DONE 0.4s
    
    #24 [tooler cacher 3/3] RUN ...
    #24 DONE 1.8s
    
    #25 [validator runner 2/4] RUN ...
    #25 CACHED
    
    #26 [validator runner 1/4] COPY ...
    #26 CACHED
    
    #27 [validator prepper 1/2] RUN ...
    #27 CACHED
    
    #28 [validator runner 4/4] RUN ...
    #28 CACHED
    
    #29 [validator prepper 2/2] RUN ...
    #29 CACHED
    
    #30 [validator validator 1/2] COPY ...
    #30 CACHED
    
    #31 [validator runner 3/4] COPY ...
    #31 ...
    
    #32 [validator validator 2/2] RUN ...
    #32 CACHED
    
    #31 [tooler runner 3/4] COPY ...
    #31 sha256:7a12bba5e2bfa8f8d5e49ee7d73caf790c094db58c1024e7a0717150af655ee4 935B / 935B 0.1s done
    #31 sha256:5f9e1834e0f8197730c572bbe669923a5345b9e973388cfa6fae3698bf3d7b54 499B / 499B 0.1s done
    #31 extracting sha256:5f9e1834e0f8197730c572bbe669923a5345b9e973388cfa6fae3698bf3d7b54 0.0s done
    #31 sha256:e69357ad55c02134eda4ec7d0a00212e71b08fb04f26e7a4140619446d834566 19.92MB / 105.72MB 0.3s
    #31 sha256:e69357ad55c02134eda4ec7d0a00212e71b08fb04f26e7a4140619446d834566 35.65MB / 105.72MB 0.5s
    #31 sha256:e69357ad55c02134eda4ec7d0a00212e71b08fb04f26e7a4140619446d834566 51.38MB / 105.72MB 0.6s
    #31 sha256:e69357ad55c02134eda4ec7d0a00212e71b08fb04f26e7a4140619446d834566 61.87MB / 105.72MB 0.8s
    #31 sha256:e69357ad55c02134eda4ec7d0a00212e71b08fb04f26e7a4140619446d834566 82.84MB / 105.72MB 0.9s
    #31 sha256:e69357ad55c02134eda4ec7d0a00212e71b08fb04f26e7a4140619446d834566 93.32MB / 105.72MB 1.1s
    #31 sha256:e69357ad55c02134eda4ec7d0a00212e71b08fb04f26e7a4140619446d834566 105.72MB / 105.72MB 1.2s
    #31 sha256:e69357ad55c02134eda4ec7d0a00212e71b08fb04f26e7a4140619446d834566 105.72MB / 105.72MB 1.2s done
    #31 extracting sha256:e69357ad55c02134eda4ec7d0a00212e71b08fb04f26e7a4140619446d834566
    #31 ...
    
    #32 [validator validator 2/2] RUN ...
    #32 sha256:50ccb1c0b1d2c10c940a1cc80601f83d0d8bf9973aad30fcba2b2bc8b0263f07 35.25MB / 35.25MB 0.4s done
    #32 DONE 2.2s
    
    #33 [validator] exporting to image
    #33 exporting layers done
    #33 exporting manifest sha256:cc2a7afcab5904c8c4b924d85b94acd11cc908f70a271bb9ffe0186ceb260779 0.0s done
    #33 exporting config sha256:d7ea765e42cbd506b8d738e28bf136deec22dd4b6c0cb6cb31efe3ef7ad0ad86 0.0s done
    #33 pushing layers
    #33 ...
    
    #34 [tooler cacher 2/3] COPY ...
    #34 CACHED
    
    #35 [tooler cacher 3/3] RUN ...
    #35 CACHED
    
    #36 [tooler runner 2/4] RUN ...
    #36 CACHED
    
    #37 [tooler runner 3/4] COPY ...
    #37 CACHED
    
    #38 [tooler runner 1/4] COPY ...
    #38 CACHED
    
    #39 [tooler cacher 1/3] COPY ...
    #39 CACHED
    
    #31 [validator runner 3/4] COPY ...
    #31 extracting sha256:e69357ad55c02134eda4ec7d0a00212e71b08fb04f26e7a4140619446d834566 2.8s done
    #31 extracting sha256:7a12bba5e2bfa8f8d5e49ee7d73caf790c094db58c1024e7a0717150af655ee4 0.0s done
    #31 DONE 5.9s
    
    #40 [tooler runner 4/4] RUN ...
    #40 0.141 Reading package lists...
    !!!BUSTED_HERE!!!
    #40 DONE 253.4s
    
    Bake definition
    {
      "group": {
        "default": {
          "targets": [
            "validator",
            "tooler"
          ]
        }
      },
      "target": {
        "tooler": {
          "attest": [
            "type=provenance,disabled=true"
          ],
          "context": ".",
          "dockerfile": "Dockerfile",
          "args": {
            "DOCKER_META_IMAGES": "<URL>/<REPO>",
            "DOCKER_META_VERSION": "<BRANCH>-tooler"
          },
          "tags": [
            "<URL>/<REPO>:<BRANCH>-tooler"
          ],
          "cache-from": [
            "type=registry,ref=<URL>/<REPO>:main-tooler.cache",
            "type=s3,blobs_prefix=cache/<REPO>/,manifests_prefix=cache/<REPO>/,region=eu-west-2,bucket=<BUCKET>"
          ],
          "cache-to": [
            "type=s3,blobs_prefix=cache/<REPO>/,manifests_prefix=cache/<REPO>/,region=eu-west-2,bucket=<BUCKET>,mode=max"
          ],
          "target": "tooler",
          "output": [
            "type=image,push=true,push=true,push=true,push=true,push=true"
          ],
          "pull": true,
          "no-cache": false
        },
        "validator": {
          "attest": [
            "type=provenance,disabled=true"
          ],
          "context": ".",
          "dockerfile": "Dockerfile",
          "args": {
            "DOCKER_META_IMAGES": "<URL>/<REPO>",
            "DOCKER_META_VERSION": "<BRANCH>-validator"
          },
          "tags": [
            "<URL>/<REPO>:<BRANCH>-validator"
          ],
          "cache-from": [
            "type=registry,ref=<URL>/<REPO>:main-tooler.cache",
            "type=s3,blobs_prefix=cache/<REPO>/,manifests_prefix=cache/<REPO>/,region=eu-west-2,bucket=<BUCKET>"
          ],
          "cache-to": [
            "type=s3,blobs_prefix=cache/<REPO>/,manifests_prefix=cache/<REPO>/,region=eu-west-2,bucket=<BUCKET>,mode=max"
          ],
          "target": "validator",
          "output": [
            "type=image,push=true,push=true,push=true,push=true"
          ],
          "pull": true,
          "no-cache": false
        }
      }
    }
  4. dud225 commented on Jun 25, 2024

    @dud225

    I've stumbled on this issue as well.
    However when switching to a different cache backend I observe the following behaviour:

    • when caching each target to its own storage area I see that there is never any cache miss:
    {
      "group": {
        "default": {
          "targets": [
            "lts",
            "lts-alpine"
          ]
        }
      },
      "target": {
        "lts": {
          "context": ".",
          "dockerfile": "Dockerfile",
          "args": {
            "NODE_VERSION": "lts"
          },
          "tags": [
            "test"
          ],
          "cache-from": [
            "type=registry,ref=myregistry/container-build-cache:test"
          ],
          "cache-to": [
            "type=registry,mode=max,ref=myregistry/container-build-cache:test,image-manifest=true,oci-mediatypes=true"
          ]
        },
        "lts-alpine": {
          "context": ".",
          "dockerfile": "Dockerfile",
          "args": {
            "NODE_VERSION": "lts-alpine"
          },
          "tags": [
            "test-alpine"
          ],
          "cache-from": [
            "type=registry,ref=myregistry/container-build-cache:test-alpine"
          ],
          "cache-to": [
            "type=registry,mode=max,ref=myregistry/container-build-cache:test-alpine,image-manifest=true,oci-mediatypes=true"
          ]
        }
      }
    }
    
    • when caching each target to the same storage area I see that one of the targets always gets a cache miss:
      "target": {
        "lts": {
          [...]
          "cache-from": [
            "type=registry,ref=myregistry/container-build-cache:test"
          ],
          "cache-to": [
            "type=registry,mode=max,ref=myregistry/container-build-cache:test,image-manifest=true,oci-mediatypes=true"
          ]
        },
        "lts-alpine": {
          [...]
          "cache-from": [
            "type=registry,ref=myregistry/container-build-cache:test"
          ],
          "cache-to": [
            "type=registry,mode=max,ref=myregistry/container-build-cache:test,image-manifest=true,oci-mediatypes=true"
          ]
        }
      }
    }
    

    That makes me understand that multiple targets steps on each other's toes when not explicitely configured to use a separate storage area.
    So I assume that the default internal cache stores all the data in the same storage area and is not suited for use with multiple targets.

  5. ruffsl commented on Sep 9, 2024

    @ruffsl

    @dud225 or @weber-software , have you tried updating the dockerfile version used? For me, updating from v1.7 to v1.9 may have resolved the superfluous cache layer busting I've encountered!

    From the change log, I suspect this may have helped in dealing with my more advanced Dockerfile staging DAG:

  6. nigelgbanks commented on Jan 29, 2025

    @nigelgbanks

    when caching each target to its own storage area I see that there is never any cache miss

    Unfortunately, I see cache misses even when using a different storage location for each image.

    I should say my setup is slightly different in that many of my targets, use other target as contexts. Something like the following.

    target "s1" {
      context = "./s1"
    }
    target "s2" {
      context = "./s2"
      contexts = {
        s1 = "target:s1"
      }
    }
    target "s3" {
      context = "./s3"
      contexts = {
        s2 = "target:s2"
      }
    }
  7. c0m1c5an5 commented on May 8, 2025

    @c0m1c5an5

    Unfortunately, I see cache misses even when using a different storage location for each image.

    I should say my setup is slightly different in that many of my targets, use other target as contexts. Something like the following.

    target "s1" {
    context = "./s1"
    }
    target "s2" {
    context = "./s2"
    contexts = {
    s1 = "target:s1"
    }
    }
    target "s3" {
    context = "./s3"
    contexts = {
    s2 = "target:s2"
    }
    }

    Indeed, I have the same setup, which also misses. By trial and error, I found that whenever there are at least two targets that use the same target as context, cache does not seem to work. I have two bake jsons attached, single dependant works properly, while having two or more breaks caching. It is for sure not the fault of the additional target between the jsons as using a different, or even more produces the same result. I will try to create a minimal reproduction repo.

    bake-one-dependant.json
    bake-two-dependants.json

  8. c0m1c5an5 commented on May 8, 2025

    @c0m1c5an5

    This is not as minimal as I wanted, but still pretty small.
    This reproduction repo can be used to get the described issue, follow the instructions in the readme.
    It is fully reproducible with my environment and GitLab registry.
    https://github.com/c0m1c5an5/buildx-414-reproducer

  9. eatdrinksleepcode commented on Jul 23, 2025

    @eatdrinksleepcode

    I am also experiencing this problem. I am also using some targets as contexts for others, so running the targets one at a time doesn't even help: as soon as I run the top-level target, the lower-level targets miss their cache (even if they have just run).

    This unfortunately kills most of the value proposition that I was looking to get from moving to bake.

  10. tonistiigi commented on Jul 24, 2025

    @tonistiigi
    Member

    @c0m1c5an5 Seem to work fine for me

     /tmp/buildx-414-reproducer [main *]
    » docker buildx prune -af                                                                                                                                                                                                                    
    ID						RECLAIMABLE	SIZE		LAST ACCESSED
    
    Total:	125.4MB
    
     /tmp/buildx-414-reproducer [main *]
    » CI_COMMIT_BRANCH=foo CI_REGISTRY_IMAGE=192.168.215.6:5000/foo/bar docker buildx bake --progress=plain                                                                                                                                      
    #0 building with "focused_mcnulty" instance using docker-container driver
    
    #1 [internal] load local bake definitions
    #1 reading docker-bake.hcl 1.82kB / 1.82kB done
    #1 DONE 0.0s
    
    #2 [base-arm64 internal] load build definition from Dockerfile
    #2 transferring dockerfile: 589B done
    #2 DONE 0.0s
    
    #3 [base-arm64 context pkg-cache] load .dockerignore
    #3 transferring pkg-cache: 2B done
    #3 DONE 0.0s
    
    #4 [base-arm64 context base] load metadata for quay.io/almalinuxorg/9-minimal:9.6-20250712
    #4 DONE 1.4s
    
    #5 [aws-cli-arm64 internal] load build definition from Dockerfile
    #5 transferring dockerfile: 575B done
    #5 DONE 0.0s
    
    #6 [docker-cli-arm64 internal] load build definition from Dockerfile
    #6 transferring dockerfile: 583B done
    #6 DONE 0.0s
    
    #7 [aws-cli-arm64 internal] load .dockerignore
    #7 transferring context: 2B done
    #7 DONE 0.0s
    
    #8 [docker-cli-arm64 internal] load .dockerignore
    #8 transferring context: 2B done
    #8 DONE 0.0s
    
    #3 [docker-cli-arm64 context pkg-cache] load .dockerignore
    #3 transferring pkg-cache: 2B done
    #3 DONE 0.0s
    
    #9 [docker-cli-arm64] importing cache manifest from 192.168.215.6:5000/foo/bar/docker-cli:cache-arm64-main-fad58de7366495db4650cfefac2fcd61
    #9 ERROR: failed to configure registry cache importer: 192.168.215.6:5000/foo/bar/docker-cli:cache-arm64-main-fad58de7366495db4650cfefac2fcd61: not found
    
    #10 [aws-cli-arm64] importing cache manifest from 192.168.215.6:5000/foo/bar/aws-cli:cache-arm64-main-fad58de7366495db4650cfefac2fcd61
    #10 ERROR: failed to configure registry cache importer: 192.168.215.6:5000/foo/bar/aws-cli:cache-arm64-main-fad58de7366495db4650cfefac2fcd61: not found
    
    #11 [base-arm64 internal] load .dockerignore
    #11 transferring context: 2B done
    #11 DONE 0.0s
    
    #12 [base-arm64 context pkg-cache] load from client
    #12 DONE 0.0s
    
    #13 [base-arm64] importing cache manifest from 192.168.215.6:5000/foo/bar/base:cache-arm64-main-fad58de7366495db4650cfefac2fcd61
    #13 ERROR: failed to configure registry cache importer: 192.168.215.6:5000/foo/bar/base:cache-arm64-main-fad58de7366495db4650cfefac2fcd61: not found
    
    #14 [base-arm64 context base] quay.io/almalinuxorg/9-minimal:9.6-20250712
    #14 resolve quay.io/almalinuxorg/9-minimal:9.6-20250712
    #14 ...
    
    #15 [docker-cli-arm64] importing cache manifest from 192.168.215.6:5000/foo/bar/docker-cli:cache-arm64-foo-acbd18db4cc2f85cedef654fccc4a4d8
    #15 inferred cache manifest type: application/vnd.oci.image.manifest.v1+json done
    #15 DONE 0.0s
    
    #16 [aws-cli-arm64] importing cache manifest from 192.168.215.6:5000/foo/bar/aws-cli:cache-arm64-foo-acbd18db4cc2f85cedef654fccc4a4d8
    #16 inferred cache manifest type: application/vnd.oci.image.manifest.v1+json done
    #16 DONE 0.0s
    
    #17 [base-arm64] importing cache manifest from 192.168.215.6:5000/foo/bar/base:cache-arm64-foo-acbd18db4cc2f85cedef654fccc4a4d8
    #17 inferred cache manifest type: application/vnd.oci.image.manifest.v1+json done
    #17 DONE 0.0s
    
    #14 [base-arm64 context base] quay.io/almalinuxorg/9-minimal:9.6-20250712
    #14 resolve quay.io/almalinuxorg/9-minimal:9.6-20250712 0.2s done
    #14 resolve quay.io/almalinuxorg/9-minimal:9.6-20250712 0.2s done
    #14 DONE 0.5s
    
    #12 [aws-cli-arm64 context pkg-cache] load from client
    #12 transferring pkg-cache: 125.22MB 0.5s done
    #12 DONE 1.1s
    
    #18 [aws-cli-arm64 stage-0 1/2] RUN --mount=type=cache,sharing=private,target=/var/cache/yum,id="cache:example:linux:arm64"     --mount=type=bind,from=pkg-cache,source="linux_arm64",rw=true,target=/var/cache/yum/metadata     --mount=type=tmpfs,target=/var/lib/dnf <<~~~ (touch /var/cache/yum/**...)
    #18 CACHED
    
    #19 [docker-cli-arm64 stage-0 1/2] RUN --mount=type=cache,sharing=private,target=/var/cache/yum,id="cache:example:linux:arm64"     --mount=type=bind,from=pkg-cache,source="linux_arm64",rw=true,target=/var/cache/yum/metadata     --mount=type=tmpfs,target=/var/lib/dnf <<~~~ (touch /var/cache/yum/**...)
    #19 CACHED
    
    #20 [aws-cli-arm64 stage-0 1/2] RUN --mount=type=cache,sharing=private,target=/var/cache/yum,id="cache:example:linux:arm64"     --mount=type=bind,from=pkg-cache,source="linux_arm64",rw=true,target=/var/cache/yum/metadata     --mount=type=tmpfs,target=/var/lib/dnf <<~~~ (microdnf --refresh makecache...)
    #20 CACHED
    
    #21 [docker-cli-arm64] exporting to image
    #21 exporting layers done
    #21 exporting manifest sha256:49eef09a3aaac3a930665ec1a174953be2f1c7e23a4fd8a3f3f72d4f583cfa52 0.0s done
    #21 exporting config sha256:ad3a807a69fe6f6dddc0c012de5166f8cf24eba60569ce06928a6e599bf1fc22 0.0s done
    #21 exporting attestation manifest sha256:76ff5a758ecaa2a8e3d0aaa03ea3496083d6a06ef73ea315d96dc9fa552cc84b
    #21 exporting attestation manifest sha256:76ff5a758ecaa2a8e3d0aaa03ea3496083d6a06ef73ea315d96dc9fa552cc84b 0.0s done
    #21 exporting manifest list sha256:dbec8ca36783db1dd21b72f1ede64b7d553694f536e0c2a27d262b56d9ed8eae done
    #21 pushing layers 0.0s done
    #21 pushing manifest for 192.168.215.6:5000/foo/bar/docker-cli:arm64-manual@sha256:dbec8ca36783db1dd21b72f1ede64b7d553694f536e0c2a27d262b56d9ed8eae 0.0s done
    #21 DONE 0.1s
    
    #22 [aws-cli-arm64] exporting to image
    #22 exporting layers done
    #22 exporting manifest sha256:6c7ba7fad41e69b6a39c3a9d55f19c057b8987545067ea6d72de040715f15437 done
    #22 exporting config sha256:81b01de3d4a4e343eb79f3fff1b876c5e19df96961897b6ec17de60fe3514e4c done
    #22 exporting attestation manifest sha256:e826c7b5ebec6dc2f61fde1f55c22e633ba141f082bfdeef046136abcfce2886 0.0s done
    #22 exporting manifest list sha256:611e942f92d636fa88a540e9b46da35acd98769422bdaaeca2cf6e63a840ee92 0.0s done
    #22 pushing layers 0.0s done
    #22 pushing manifest for 192.168.215.6:5000/foo/bar/aws-cli:arm64-manual@sha256:611e942f92d636fa88a540e9b46da35acd98769422bdaaeca2cf6e63a840ee92 0.0s done
    #22 DONE 0.1s
    
    #23 [aws-cli-arm64] exporting cache to registry
    #23 preparing build cache for export 0.0s done
    #23 writing layer sha256:037a18dc9bab6e5d31a4bad9d1255b4afd0cdd794cd7902ddc6130cec22582fe done
    #23 writing layer sha256:42f5d853d1caea70225a55a34c087fc31f6003c0beed918b5ec8d6bccee8dcc5 done
    #23 writing layer sha256:6fb3e9edf299163f60d5900f83b42797482e2674736e25dadc2a8b82480e4369 done
    #23 writing config sha256:e3ebe95cb5dda396fe16bb3a0da391dbf94c856de8950e8ee0fdeb5a94c2a34a done
    #23 writing cache image manifest sha256:2487c72ab5a8bb7f98af43b61e9bb954841788467e8580e88da1340e2e5a8a2e done
    #23 DONE 0.0s
    
    #24 [docker-cli-arm64] exporting cache to registry
    #24 preparing build cache for export 0.0s done
    #24 writing layer sha256:42f5d853d1caea70225a55a34c087fc31f6003c0beed918b5ec8d6bccee8dcc5 done
    #24 writing layer sha256:61f2b351623259f0f1c9f907f2c17da71dbb7179df4880979937448a9df61ee4 done
    #24 writing layer sha256:6fb3e9edf299163f60d5900f83b42797482e2674736e25dadc2a8b82480e4369 done
    #24 writing config sha256:567a2ab5a972464d5b61ddb6bf460635ecf3804e7cc85e838d15f66f07c87d03 done
    #24 writing cache image manifest sha256:75d75a5aac03d50f00ab262e063e61c3dba4da50cff22116cc297f9aaf2b8d09 done
    #24 DONE 0.0s
    
    #25 [base-arm64] exporting to image
    #25 exporting layers done
    #25 exporting manifest sha256:1141c216044e69c83035ac95f7a82dd66558ddecba1aa9352c3e95cb5cce0800 done
    #25 exporting config sha256:fda590a6516f8ed3d87d8999dff6236318bf36a39ba4e422c04529410dd806f4 0.0s done
    #25 exporting attestation manifest sha256:bd9ba7710d2c7d8a4438c19e12d6eecd498171bf4ae8c2fdf2eac41bd7ca9d1f 0.0s done
    #25 exporting manifest list sha256:cac766824a4508556ccbbc115062794a2916aa13c787bf17a545f42f20a9c840 0.0s done
    #25 pushing layers 0.0s done
    #25 pushing manifest for 192.168.215.6:5000/foo/bar/base:arm64-manual@sha256:cac766824a4508556ccbbc115062794a2916aa13c787bf17a545f42f20a9c840 0.0s done
    #25 DONE 0.1s
    
    #26 [base-arm64] exporting cache to registry
    #26 preparing build cache for export 0.0s done
    #26 writing layer sha256:42f5d853d1caea70225a55a34c087fc31f6003c0beed918b5ec8d6bccee8dcc5 done
    #26 writing layer sha256:6fb3e9edf299163f60d5900f83b42797482e2674736e25dadc2a8b82480e4369 done
    #26 writing config sha256:c39130d4edb9b1fa3121466e6e92fd7804101c7aa8b06f2613543582b99a1d72 done
    #26 writing cache image manifest sha256:446393225ba121516a7695184123c90581b9ebeff0ffb71df5f76ef7eb87d639 done
    #26 DONE 0.0s
    ------
     > [aws-cli-arm64] importing cache manifest from 192.168.215.6:5000/foo/bar/aws-cli:cache-arm64-main-fad58de7366495db4650cfefac2fcd61:
    ------
    ------
     > [docker-cli-arm64] importing cache manifest from 192.168.215.6:5000/foo/bar/docker-cli:cache-arm64-main-fad58de7366495db4650cfefac2fcd61:
    ------
    ------
     > [base-arm64] importing cache manifest from 192.168.215.6:5000/foo/bar/base:cache-arm64-main-fad58de7366495db4650cfefac2fcd61:
    ------
    
    View build details:
      aws-cli-arm64: docker-desktop://dashboard/build/focused_mcnulty/focused_mcnulty0/nlmuyf3zdstfzz31bs6rrcigb
      base-arm64: docker-desktop://dashboard/build/focused_mcnulty/focused_mcnulty0/n17ioin9n0qmozfk16den8jbb
      docker-cli-arm64: docker-desktop://dashboard/build/focused_mcnulty/focused_mcnulty0/i4w99lp5czby2kpz7zwh8tjso
    
    

    I guess you are expecting the (touch /var/cache/yum/**...) lines to not match cache. I did need to add microdnf-- refresh makecache to the install commands as the pkg-cache seems to be stale, but (unless it is a pure timing thing) I doubt the actual command needs to be exact.

  11. rcwbr commented on Nov 2, 2025

    @rcwbr

    @c0m1c5an5 / @nigelgbanks I hit this issue as well, and boiled it down to a fairly minimal case before finding this thread, as well as a workaround (basically adding otherwise-unused target:[target] contexts pointing to each target that the selected target(s) depend on, including transitively). I hope this is helpful!

    @tonistiigi I also was able to verify that my example only manifests this issue when using remote registries (or at least does not reproduce using a local distribution registry). In your log, it looks like your registry is at least LAN-local -- so I'm curious what you'd see if using a remote registry... and if this all may come down to timing.

  12. nbanmp commented on Dec 15, 2025

    @nbanmp

    @rcwbr I unfortunately am also seeing this issue even when adding all the contexts you described. I also can confirm that I only see this with remote registries.

    My workaround was different. I'm adding the base image as an explicit cache-from to the app target.

    # Parent target - builds a base image with some packages installed
    target "base" {
      context    = "./context"
      dockerfile = "Dockerfile.base"
      tags       = ["${REGISTRY}/base:${TAG}"]
    
      cache-from = ["type=registry,ref=${REGISTRY}/cache-base:latest"]
      cache-to   = ["type=registry,ref=${REGISTRY}/cache-base:latest,mode=max,oci-mediatypes=true,image-manifest=true"]
    }
    
    # Child target - depends on base via target: reference
    # This is where the cache bug manifests
    target "app" {
      context    = "./context"
      dockerfile = "Dockerfile.app"
      tags       = ["${REGISTRY}/app:${TAG}"]
    
      # This creates the problematic dependency
      contexts = {
        baseimg = "target:base"
      }
    
      cache-from = [
        "type=registry,ref=${REGISTRY}/cache-base:latest", # FAILED WITHOUT THIS LINE
        "type=registry,ref=${REGISTRY}/cache-app:latest"
      ]
      cache-to   = ["type=registry,ref=${REGISTRY}/cache-app:latest,mode=max,oci-mediatypes=true,image-manifest=true"]
    }
    
  13. c0m1c5an5 commented on Jan 1, 2026

    @c0m1c5an5

    @rcwbr and @nbanmp Sadly (or maybe it is good actually), neither mine nor your examples are currently reproducible for me any more. It is either that a fix has been merged that unintentionally fixes this issue, or the GitLab registry, which I used for all experiments made some changes.

    In any case, our current reproducer approach is incomplete, as we rely on an external registry. So the real reproducer should use a local registry started with docker run -d -p 5000:5000 --restart always --name registry registry:3. It is also possible that the characteristics of the connection quality play a role here, which can be simulated by tc using for example https://github.com/lukaszlach/docker-tc. So the proper reproducer should include the instructions on how to start a local registry and use it to store the cache.

    Currently the issue does not reproduce for me, but in case it still exists, please create a reproducer with a local registry. This will in addition allow us to cross check if, maybe, the issue is in the registry implementation of the provider.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions