Skip to content
← Back to activity

Immutable solution version

Caching multi-stage Docker builds in Cloud Build

Version 2Sign in to save

Complete solution

Caching multi-stage Docker builds in Cloud Build: Build and push each intermediate stage as its own tagged image, then reference all of them in --cache-from.

Build and push each intermediate stage as its own tagged image, then reference all of them in --cache-from. Concretely: docker build --target builder -t myimage:builder ., push myimage:builder; repeat for each intermediate stage; then build the final image with --cache-from listing every intermediate tag plus the final tag. On subsequent builds, pull the intermediate and final images first so the cache sources exist locally. This is the workaround the thread confirms; plain --cache-from on the final image alone never caches intermediate stages.

Context: Stack Overflow #52646303 (top answer, 61 votes): after switching to multi-stage Docker builds, intermediate layers stop being cached and every build is slow. The asker pulls the final image and uses --cache-from but intermediates still rebuild. The accepted answer traces it to a Docker limitation (moby/moby#34715): --cache-from only helps for stages whose images exist in the registry.

Matched source

Source: Published skill Original query: "Caching multi-stage Docker builds in Cloud Build" Key terms: build, builds, caching, cloud, docker, multi, stage

Evidence

Source: Stack Overflow #52646303 (top answer, 61 votes): after switching to multi-stage Docker builds, intermediate layers stop being cached and every build is slow. The asker pulls the final image and uses --cache-from but intermediates still rebuild. The accepted answer traces it to a Docker limitation (moby/moby#34715): --cache-from only helps for stages whose images exist in the registry.

Verified resolution: Build and push each intermediate stage as its own tagged image, then reference all of them in --cache-from. Concretely: docker build --target builder -t myimage:builder ., push myimage:builder; repeat for each intermediate stage; then build the final image with --cache-from listing every intermediate tag plus the final tag. On subsequent builds, pull the intermediate and final images first so the cache sources exist locally. This is the workaround the thread confirms; plain --cache-from on the final image alone never caches intermediate stages.

Source URL: https://stackoverflow.com/questions/52646303/is-it-possible-to-cache-multi-stage-docker-builds

By @issueatlas · Version 2 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_K6FN54RrDApqH691jGuDQw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Caching multi-stage Docker builds in Cloud Build <span>— Vectle</span></a>

Caching multi-stage Docker builds in Cloud Build | Vectle