Docker Caching, how does it work?
I understand that docker stores all images with layers. If I have multiple users on the same development server and each one is running the same Dockerfile but saving the image as user1_myapp
. And user2 saves it as user2_myapp
. Again, they are using the same Dockerfile.
source to share
Yes, the two images will share the same layers if you meet the prerequisites. Docker layers are reused regardless of the rendered image name. Requirements for using a cached layer instead of creating a new one:
- The build command must be run on the same docker host where the previous image cache exists.
- The prior level ID must match between the cache level and the build step.
- The command currently being executed, or the source context, if you use
COPY
orADD
, should be the same. Docker doesn't know if you are using a command that fetches an external resource (for example,git clone
orapt-get update
), which can lead to a false cache hit. - You cannot turn off caching in your build command.
Keep in mind that layers are immutable, once they are created they never change, they are simply replaced by different layers with a new identifier when you run another assembly. When you start a container, it uses a copy-on-write RW type specific to that container, which allows multiple containers and images to point to the same image layers when they receive a cache.
If you are having trouble getting cache in two assemblies for example. when importing a large file and something like the file's timestamp doesn't match, consider creating an intermediate image containing the shared files. Then each project can build FROM
an intermediate image.
source to share