I understand that Docker --cache-from command will restore cache from pulled images when building a different one. Am I wrong? Whenever I create a new image, delete it and its dangling cache, pull it and build it again, it will not use the newly downloaded image as cache. Below are the commands for my use case.
docker build --target base -t image:base .
docker push image:base
docker image rm image:base
docker builder prune
docker pull image:base
docker build --target base --cache-from image:base -t image:base .
If I don't prune the cache, it will use it regardless of the --cache-from command being present or not. Therefore, how am I supposed to use --cache-from, and is there any possibility of restoring cache from pulled images without using docker load (because it takes a while)?
CodePudding user response:
With buildkit, there's an additional setting to include cache metadata in the created image:
--build-arg BUILDKIT_INLINE_CACHE=1
That only handles the final image layers. If you want to include the cache for intermediate stages of a multi stage build, you likely want to cache to a registry, which I believe needs buildx to access these buildkit options.
--cache-from type=registry,ref=localhost:5000/myrepo:buildcache
--cache-to type=registry,ref=localhost:5000/myrepo:buildcache,type=max
Buildkit defines several other cache options in their readme.