I'd like to add another important point to the answer
Wrong answer
--build-arg BUILDKIT_INLINE_CACHE=1 caches only the last layer, and works only in cases when nothing is changed in the whole Dockerfile
So, to enable the caching of layers for the whole build, this argument should be replaced by --cache-to type=inline,mode=max. See the documentation
Correct answer
The documentation above as on 2023-03-28 states:
When generating a cache output, the --cache-to argument accepts a mode option for defining which layers to include in the exported cache. This is supported by all cache backends except for the inline cache.
That means, for the cached intermediate states with all layers one needs to use registry cache backend.
I plan to use the same image:tag as pushed at the build time, but with suffix -buildcache. So, --cache-from type=registry,ref=org/image:tag-buildcache,mode=max --cache-to type=registry,ref=org/image:tag-buildcache,mode=max