packagemain.tech

packagemain.tech

How to Speed Up Docker Builds with Build Cache

How to store and share Docker build cache across teams using Docker registry.

Julien Singler's avatar
Julien Singler
Aug 01, 2024
∙ Paid

Introduction

Nowadays, Docker is a go-to tool for building, shipping, and running containerized applications. One of the challenges a developer may face is build times, especially for large and complex codebases.

Docker build cache can offer a powerful solution to this problem by allowing you to reuse previously built layers.

In this article we will explore how to create and store build cache for different stages, such as the builder stage, and how to share this cache with your team using Docker Registry.

Understanding Docker Build Cache

Docker build cache is a mechanism that allows Docker to reuse layers from previous builds. Each instruction in a Dockerfile creates a new layer, and Docker caches these layers to avoid redundant work. When you rebuild an image, Docker checks if it can reuse any of the cached layers, which can drastically reduce build times.

Multi-Stage Builds and Caching

Multi-stage builds are a powerful feature in Docker that allows you to use multiple FROM statements in your Dockerfile. This enables you to create intermediate stages, such as a builder stage, which can be cached and reused independently. By caching these stages, you can further optimise your build process.

Setting Up Docker Build Cache

Step 1: Create a Multi-Stage Dockerfile

First, create a Dockerfile with multiple stages. Here's an example:

# Stage 1: Builder
FROM node:14 AS builder
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
RUN npm run build

# Stage 2: Production
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
User's avatar

Continue reading this post for free, courtesy of Alex Pliutau.

Or purchase a paid subscription.
© 2026 Aliaksandr Pliutau · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture