Dockerfile RUN --mount=type=cache checklist

  • New Docker Engine (>= v23) for BuildKit.
  • Add directive # syntax = docker/dockerfile:1.2.
  • Don't test with --no-cache.
  • Run mkdir before mount.
  • --mount= is a RUN command option rather than the command itself, prefix it before every command that's relevant.

Recipe for yarn:

# To get cache dir of specific yarn installation:
# RUN yarn cache dir
RUN mkdir -p /usr/local/share/.cache/yarn
RUN --mount=type=cache,target=/usr/local/share/.cache/yarn yarn