Does removing docker image affect the caching

Viewed 692

At the end of a Jenkins pipeline there is a
sh "docker image rm #{IMAGE_ID_HERE}"
I noticed that on each build the cache is not utilized, and all the steps are executed again. Could this be the reason why the cache is not utilized?

1 Answers

docker image rm command will delete the image build during pipeline. for that reason when you delete the image, next time the image will be build again when it comes to docker build command. So in this case, Yes this command is the reason for not caching docker image for next build.

If you want to use cache for docker steps please use docker build command with --cache-from option, and remove this command. i-e docker image rm

I assume you are deleting the image at the end of pipeline, just to free space and resources, I would suggest not to delete them after every build, but run a cron job in your build agent that runs at stipulated time e.g maybe every night at 12AM and in that cron job you can run docker image prune command which will delete all unused/dangling images.

Related