3

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)?

1 Answer 1

4

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,mode=max

Buildkit defines several other cache options in their readme.

Sign up to request clarification or add additional context in comments.

5 Comments

This works like a charm now except for the issue with intermediate stages. Basically, if stage 1 has one layer changed, I see that stage 2 (FROM 1 as 2) will ignore caching for itself even if I add --cache-from 1 and 2 or even if I build stage 1 separately. The only way to get the cache for stage2 is to configure the --cache-to options? (which docker does not have enabled)
@MorganSoren you can switch to docker buildx build to get access to those options.
sorry, what I meant is this "docker driver currently only supports exporting inline cache metadata to image configuration." confirming I cant use --cache-to unless I'm missing something important.
@MorganSoren if you're using buildx with the cache options, you don't need/want that build-arg definition.
I will address the issue in more detail in a new question. The main problem has been solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.