Fix --mount=type=cache with onbuild steps
James McDonnell
We are using a shared image the cache mount in the dockerfile:
ONBUILD RUN --mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
apt update ...
This image is then used as the root image in another dockerfile.
When this second docker image is build, it fails because of:
failed to solve: cannot mount from stage "scratch" to "/var/lib/apt/lists", stage needs to be defined before current command
A work around was found by defining
FROM scratch as scratch
and FROM parent as parent
in the child docker image, but this is cumbersome. The build should work just like it does locally.D
Dale Phillip
This is a pretty annoying edge case. The
FROM scratch AS scratch
workaround seems to fix the build, but it definitely feels unnecessary for a Dockerfile that already works locally. Hopefully CircleCI can handle the ONBUILD --mount=type=cache
stage correctly so builds stay consistent across environments. It’s a bit like using Truecaller apps when you want to avoid uncertainty—having the environment behave predictably makes things much easier.L
Lucas Carlos
This looks like a useful fix, especially for workflows that rely on ONBUILD steps and cache behavior. Improving consistency around mount type caching could make builds more predictable and reduce unexpected issues in CI pipelines. Hopefully this gets some attention from the team.
J
Jack Bravo
A good temporary workaround is to avoid using
ONBUILD
with --mount=type=cache
until this is supported correctly. Moving the cache mount into the child Dockerfile or defining the required build stages explicitly (such as FROM scratch AS scratch
) should prevent the build error. Hopefully this can be fixed in CircleCI so it behaves the same way as a local Docker build.