docker / buildx

Docker CLI plugin for extended build capabilities with BuildKit
Apache License 2.0
3.56k stars 481 forks source link

bake cache misses with multiple targets #414

Open weber-software opened 4 years ago

weber-software commented 4 years ago

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:

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

weber-software commented 3 years ago

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

ruffsl commented 6 months ago

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=/:.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 #13 DONE 0.0s #14 [validator] importing cache manifest from /:.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 ```json { "group": { "default": { "targets": [ "validator", "tooler" ] } }, "target": { "tooler": { "attest": [ "type=provenance,disabled=true" ], "context": ".", "dockerfile": "Dockerfile", "args": { "DOCKER_META_IMAGES": "/", "DOCKER_META_VERSION": "-tooler" }, "tags": [ "/:-tooler" ], "cache-from": [ "type=registry,ref=/:main-tooler.cache", "type=s3,blobs_prefix=cache//,manifests_prefix=cache//,region=eu-west-2,bucket=" ], "cache-to": [ "type=s3,blobs_prefix=cache//,manifests_prefix=cache//,region=eu-west-2,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": "/", "DOCKER_META_VERSION": "-validator" }, "tags": [ "/:-validator" ], "cache-from": [ "type=registry,ref=/:main-tooler.cache", "type=s3,blobs_prefix=cache//,manifests_prefix=cache//,region=eu-west-2,bucket=" ], "cache-to": [ "type=s3,blobs_prefix=cache//,manifests_prefix=cache//,region=eu-west-2,bucket=,mode=max" ], "target": "validator", "output": [ "type=image,push=true,push=true,push=true,push=true" ], "pull": true, "no-cache": false } } } ```
dud225 commented 4 months ago

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

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.

ruffsl commented 2 months ago

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