4

I few months ago I decided to setup the CI of my project building docker images with the no-cache flag: I thought it would be best not to take the risk of letting docker use an old cache layer.

I realized only now that the sha of the layers of my image are always different (even if the newly built image should generate a layer identical to the previous built) and whenever I pull the newly built image all layers are always downloaded from zero.

I'm thinking now that the issue is the --no-cache flag, I know it sounds obvious, but honestly I thought that the --no-cache was only slower to execute, but also thought that it was implemented in a functional way (same command + same content = same layer).

Can someone confirm that the --no-cache flag is the problem?

1 Answer 1

4

The thing with containers is that practically speaking, you will never build the same layer with the same sha, ever. You can only have the same sha if you use the same layer you previously built.

Think about it this way: every time you build a layer, there will be at least a log file, a timestamp, something that is different - and then we have not yet mentioned external dependencies pulled in.

The --no-cache flag will simply stop the Docker engine from using the cached layers and it will download & build everything again. So that flag is indeed the (indirect) reason why your hashes are different, but that is the intended behavior. Building from the cache means that your builds will be faster, but reuse previously built layers, hence having the same sha (this may result in reusing previous stale results and whatnot, that is why we have the flag).

Have a look at this article for further info: https://thenewstack.io/understanding-the-docker-cache-for-faster-builds/

If you wish to guarantee some layers have the same sha but still not want to use the cache, you may want to look into multiphase Docker builds: https://docs.docker.com/develop/develop-images/multistage-build/

This way you can have a base image which is fixed and build everything else on top of that.

2
  • Something that I wonder that I haven't read anything conclusive one way or the other is: When you use --no-cache it won't USE cache, but will it CREATE cache? Will subsequent runs be able to leverage the output from the --no-cache build that was attempted?
    – Steven Lu
    Mar 9, 2022 at 22:25
  • I just tested and it does cache the runs that are run with --no-cache. Those runs just won't use any cache.
    – Steven Lu
    Mar 9, 2022 at 23:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.