DEV Community

Cover image for Build Docker images faster using build cache

Build Docker images faster using build cache

Kyle Galbraith on January 07, 2024

When working with Docker, the faster we can build an image, the quicker our development workflows and deployment pipelines can be. Docker's build c...
Collapse
 
stanleysathler profile image
Stanley Sathler • Edited

Thanks, Kyle! Re. having 2 consecutive layers, one downloading the file, one removing it - will the final image have that intermediary layer in it (with the file), or will that have no file at all as a result of the last layer only? In other words, will the final image have intermediary layer contents or does it only care about the final layer results?

Collapse
 
kylegalbraith profile image
Kyle Galbraith

Great question! A Docker image is really a series of layers all stacked on top of one another. There is an exception there with multi-stage builds, but I'll come back to that.

In the example you're referring to, yes there will be an intermediate layer in the image that contains that file. Now if you SSH into the image, will you find the file? No, as the last layer is your entry layer. But that intermediate layer does exist in the image as a whole and thus contributed to the overall size.

Multi-stage builds, i.e. using multiple FROM statements, is like building multiple images concurrently and then in your last stage you copy in files from the other images and that becomes your entire final image. The earlier stages, the other FROM steps, are thrown away and don't contribute to the final image or its size.

Collapse
 
lovestaco profile image
Athreya aka Maneshwar

Multi-stage is the best I reduced soo much junk and got a concise image in the end xD

Collapse
 
hseritt profile image
Harlin Seritt

Hi Kyle, thanks for the write-up. We've gone to doing something like this at work because our images for our angular app has gotten way too slow to build.

Collapse
 
kylegalbraith profile image
Kyle Galbraith

I'd love to learn more about that!

Collapse
 
nxquan profile image
nxquan

good