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.

Question: if the image is, for example 100mb, both images take 100mb each or they use the same image and only use 100MB instead of 200MB?
+3


source to share


1 answer


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

    or ADD

    , should be the same. Docker doesn't know if you are using a command that fetches an external resource (for example, git clone

    or apt-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.

+3


source







All Articles