Home Explore Blog CI



docker

1st chunk of `content/get-started/docker-concepts/building-images/using-the-build-cache.md`
cb9dfd16a969a8f8031563130ebb39317d6616319f34ed39000000010000100e
---
title: Using the build cache
keywords: concepts, build, images, container, docker desktop
description: This concept page will teach you about the build cache, what changes invalidate the cache and how to effectively use the build cache.
summary: |
  Using the build cache effectively allows you to achieve faster builds by
  reusing results from previous builds and skipping unnecessary steps. To
  maximize cache usage and avoid resource-intensive and time-consuming
  rebuilds, it's crucial to understand how cache invalidation works. In this
  guide, you’ll learn how to use the Docker build cache efficiently for
  streamlined Docker image development and continuous integration workflows.
weight: 4
aliases: 
 - /guides/docker-concepts/building-images/using-the-build-cache/
---

{{< youtube-embed Ri6jMknjprY >}}

## Explanation

Consider the following Dockerfile that you created for the [getting-started](./writing-a-dockerfile/) app.


```dockerfile
FROM node:20-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "./src/index.js"]
```

When you run the `docker build` command to create a new image, Docker executes each instruction in your Dockerfile, creating a layer for each command and in the order specified. For each instruction, Docker checks whether it can reuse the instruction from a previous build. If it finds that you've already executed a similar instruction before, Docker doesn't need to redo it. Instead, it’ll use the cached result. This way, your build process becomes faster and more efficient, saving you valuable time and resources.

Using the build cache effectively lets you achieve faster builds by reusing results from previous builds and skipping unnecessary work.
In order to maximize cache usage and avoid resource-intensive and time-consuming rebuilds, it's important to understand how cache invalidation works.
Here are a few examples of situations that can cause cache to be invalidated:

- Any changes to the command of a `RUN` instruction invalidates that layer. Docker detects the change and invalidates the build cache if there's any modification to a `RUN` command in your Dockerfile.

- Any changes to files copied into the image with the `COPY` or `ADD` instructions. Docker keeps an eye on any alterations to files within your project directory. Whether it's a change in content or properties like permissions, Docker considers these modifications as triggers to invalidate the cache.

- Once one layer is invalidated, all following layers are also invalidated. If any previous layer, including the base image or intermediary layers, has been invalidated due to changes, Docker ensures that subsequent layers relying on it are also invalidated. This keeps the build process synchronized and prevents inconsistencies.

When you're writing or editing a Dockerfile, keep an eye out for unnecessary cache misses to ensure that builds run as fast and efficiently as possible.

## Try it out

In this hands-on guide, you will learn how to use the Docker build cache effectively for a Node.js application.

### Build the application

1. [Download and install](https://www.docker.com/products/docker-desktop/) Docker Desktop.

2. Open a terminal and [clone this sample application](https://github.com/dockersamples/todo-list-app).


    ```console
    $ git clone https://github.com/dockersamples/todo-list-app
    ```

3. Navigate into the `todo-list-app` directory:


    ```console
    $ cd todo-list-app
    ```

    Inside this directory, you'll find a file named `Dockerfile` with the following content:


    ```dockerfile
    FROM node:20-alpine
    WORKDIR /app
    COPY . .
    RUN yarn install --production
    EXPOSE 3000
    CMD ["node", "./src/index.js"]
    ```

4. Execute the following command to build the Docker image:

    ```console
    $ docker build .
    ```

    Here’s the result of the build process:

    ```console
    [+] Building 20.0s (10/10) FINISHED
    ```

    The first line indicates that the entire build process took *20.0 seconds*. The first build may take some time as it installs dependencies.

Title: Understanding and Utilizing the Docker Build Cache
Summary
This section explains how the Docker build cache works, emphasizing its role in speeding up builds by reusing results from previous executions. It details what triggers cache invalidation, such as changes to `RUN` commands or modifications to files copied via `COPY` or `ADD`. The section also includes a practical guide on building a Node.js application to demonstrate effective use of the build cache.