does docker build --no-cache builds different layers?

Viewed 2410

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 Answers

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.

Related