Best way to cache multi-stage + multi-platform image builds #1382
-
Docs request. Basically what is the best way to cache intermediate layers for a multi-stage build for multiple platforms? Is there a way to run two completely different commands, one per platform, and then push them to the same resulting tag? We are building multi-stage and multi-platform images in Github Actions using docker/build-push-action and multiple buildkit builder nodes (Workflow job runs on a
We are building docker images for a commit, then testing it and promoting it if tests pass. There is also a The issue we are seeing is that the intermediate layers for the The full command that gets ran by Github Actions: /usr/bin/docker buildx build \
--build-arg GIT_REF=eaabf7c57706641d1c1f1fc384ec14e9333c3413 \
--cache-from ghcr.io/org/repo:buildcache-branch,ghcr.io/org/repo:buildcache-master \
--cache-to type=registry,ref=ghcr.io/org/repo:buildcache-branch,mode=max \
--file Dockerfile \
--iidfile /tmp/docker-build-push-cM103X/iidfile \
--platform linux/amd64,linux/arm64 \
--tag ghcr.io/org/repo:branch \
--tag ghcr.io/org/repo:commit-eaabf7c57706641d1c1f1fc384ec14e9333c3413 \
--target prod \
--metadata-file /tmp/docker-build-push-cM103X/metadata-file \
--push . |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
👋 Heya, sorry it took so long to get to this! This issue sounds a lot like this: #1044. Essentially, while buildx has special logic for linking together separate outputs again after push, there's no similar logic for linking the caches. We also discussed this on the community slack some time back. As a workaround in the meantime, you could perform multiple |
Beta Was this translation helpful? Give feedback.
-
I can't see the correlation between this...
... and the code @AngellusMortis provides. Could you explain a little bit further? |
Beta Was this translation helpful? Give feedback.
-
The examples provided earlier may have been in the incorrect order.
All the commands should go through a single buildkit builder as the builder building the final image needs to have the first two cached builds. Subsequent builds can use a different builder, as the cache is pushed in the last stage. |
Beta Was this translation helpful? Give feedback.
-
For anyone dealing with this on a GitHub action, you can use a matrix to prefix the cache. Works quite well for me, although support on your specific registry might vary.
|
Beta Was this translation helpful? Give feedback.
👋 Heya, sorry it took so long to get to this!
This issue sounds a lot like this: #1044. Essentially, while buildx has special logic for linking together separate outputs again after push, there's no similar logic for linking the caches. We also discussed this on the community slack some time back.
As a workaround in the meantime, you could perform multiple
build
s - because buildkit's own cache will apply for the first build, you can do your whole build + export, then have a special build step just to exportlinux/amd64
cache, then another special build step just to export thelinux/arm64
cache. As long as these are on the same set of builders, the special steps won't need to rebuild the c…