How to Reuse Cache in Multi-Stage Build with Dynamic Image Name

Is it possible to satisfy all of these requirements at once?

  1. Use the multi-stage builder pattern
  2. Use a dynamic image name in the final FROM image
  3. Don’t bust cache for all the builders with every change to the final FROM image name

It seems to bust cache for any change to the BASE_IMAGE argument, which is unnecessary and inefficient.

ARG BASE_IMAGE

##### Stage: Build cfredis library #############################################

FROM gradle:4.10-alpine as cfredis-builder

...

##### Stage: Build CFSolrLib ###################################################

FROM gradle:4.10-alpine as cfsolrlib-builder

...

##### Stage: Build DocBox ######################################################

FROM gradle:4.10-alpine as docbox-builder

...

##### Stage: Build IEC PDF Generator ###########################################

FROM maven:3.6.0 as iec-pdf-generator-builder

...

##### Stage: Get POI Libs ######################################################

FROM gradle:4.10-alpine as poi-builder

...

##### Stage: Build Redis Lucee Extension #######################################

FROM kmindi/openjdk-ant-docker as redis-extension-builder

...

##### Stage: Build TestBox #####################################################

FROM alpine:3.8.4 as testbox-builder

...

##### Stage: Build Lucee #######################################################

FROM ${BASE_IMAGE}

...

###### Include 3rd-party libs ######
COPY --from=cfsolrlib-builder /tmp/dist /app/lib/cfsolrlib
COPY --from=docbox-builder /tmp/dist /app/lib/docbox
COPY --from=iec-pdf-generator-builder /tmp/dist /app/lib/iec-pdf-generator
COPY --from=poi-builder /tmp/lib /app/lib/poi
#==================================#

Hmm, never tried to use an ARG in the FROM part of the declaration.You Dockerfile usage looks valid according the Dockerfile reference. I would expect the cache miss not to be happening on the first line, but rather in your last FROM declaration, where you actualy use the ARG the first time…

Dockerfile Reference: Understand how ARG and FROM interact
Dockerfile Reference: Impact on build caching

Though, it pretty much looks like you identified behavior not covered by the documentation, as Impact on build caching does cover the effect on RUN, but not on FROM.