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.A
Alice Sphere
It seems like you're encountering a common issue with the cache mount in ONBUILD steps. Defining the stages explicitly, as you've done, is a workaround, but it can indeed be cumbersome. Have you considered using a multi-stage popular games build with a defined base image for better compatibility? This might simplify your Dockerfile and avoid the mounting issue. Let us know if you need further assistance!
A
Alicent Hightower
@Slope Game Hey James - yeah, this is a frustrating quirk with how CircleCI handles ONBUILD and cache mounts. That workaround with naming the stages works, but it really shouldn’t be necessary if it just behaved like a local Docker build.