Home > other >  Any way to auto-detect modified lines in Dockerfile and only rebuild from the first modified line on
Any way to auto-detect modified lines in Dockerfile and only rebuild from the first modified line on

Time:10-26

I'm building a docker image using a Dockerfile, and I often have to tweak lines further down the Dockerfile. When I instruct docker to rebuild the image, it starts from the very top of the Dockerfile (installing dependencies, etc.), and works down to the actual changed lines. Is there any technology or general method to detect what the first modified line in a Dockerfile is, and to only rebuild from that point onwards?

This request is based on an assumption that a snapshot can be stored at each line in a Dockerfile. I assume this is possible because the output logs while building indicate that intermediate containers are being created at each line break.

I've tried running containers at each of the breakpoint image IDs, and it is indeed possible to look at containers running from each breakpoint (e.g. before and after installing packages, before and after pulling in a data file from a URL, etc.) But I don't know how to instruct docker to rebuild from one of these intermediate image points? Furthermore, I don't know how to list these intermediate points after an image's final form has been built.

CodePudding user response:

Usually docker does this automatically. If you don't change anything in the beginning of the Dockerfile it should just cache the intermediate layers. You can see this in the output through these lines: ---> Using cache More information on build caching can be found here.

If you are building on an CI/CD system or any other system, where the previous build cache is not available, you can externalize the cache through cache backends.

  • Related