Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Caching, how does it really work?

I understand that docker stores every images with layers. If I have multiple users on one development server, and everyone is running the same Dockerfile, but storing the image as, user1_myapp. And user2 is storing it as user2_myapp. Now again, they are using the same Dockerfile.

The question is, if the image is for example 100mb, are both images taking 100mb each, or are they sharing the same image and using only 100mb instead of 200mb?

like image 327
Serey Avatar asked Jul 21 '17 13:07

Serey


People also ask

Does Docker have a cache?

Whether it's for the next release of your software, or locally during development. Because building images is a common task, Docker provides several tools that speed up builds. The most important feature for improving build speeds is Docker's build cache.

What does cached mean Docker?

Overview. Docker layer caching (DLC) is a great feature to use if building Docker images is a regular part of your CI/CD process. DLC will save image layers created within your jobs, rather than impact the actual container used to run your job.

Where does Docker store build cache?

In a default install, these are located in /var/lib/docker. During a new build, all of these file structures have to be created and written to disk — this is where Docker stores base images. Once created, the container (and subsequent new ones) will be stored in the folder in this same area.

Does Docker pull cache?

Pulling cached imagesThe Docker daemon checks the Container Registry cache and fetches the images if it exists. If your daemon configuration includes other Docker mirrors, the daemon checks each one in order for a cached copy of the image.


1 Answers

Yes, the two images will share the same layers if you meet the prerequisites. Docker layers are reused independently of the resulting image name. The requirements to use a cached layer instead of creating a new one are:

  • The build command needs to be run against the same docker host where the previous image's cache exists.
  • The previous layer ID must match between the cache layer and the running build step.
  • The command currently being run, or the source context if you are running a COPY or ADD, must be identical. Docker does not know if you are running a command that pulls from an external changing resource (e.g. git clone or apt-get update), which can result in a false cache hit.
  • You cannot have disabled caching in your build command.

Keep in mind that layers are immutable, once created they are never changed, just replaced with different layers with new ID's when you run a different build. When you run a container, it uses a copy-on-write RW layer specific to that container, which allows multiple containers and images to point to the same image layers when they get a cache hit.

If you are having problems getting the cache to match in the two builds, e.g. importing a large file and something like the file timestamp doesn't match, consider creating an intermediate image that contains the common files. Then each project can build FROM that intermediate image.

like image 109
BMitch Avatar answered Oct 21 '22 15:10

BMitch