--- 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. 5. Rebuild without making changes. Now, re-run the `docker build` command without making any change in the source code or Dockerfile as shown: ```console $ docker build . ``` Subsequent builds after the initial are faster due to the caching mechanism, as long as the commands and context remain unchanged. Docker caches the intermediate layers generated during the build process. When you rebuild the image without making any changes to the Dockerfile or the source code, Docker can reuse the cached layers, significantly speeding up the build process. ```console [+] Building 1.0s (9/9) FINISHED docker:desktop-linux => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 187B 0.0s ... => [internal] load build context 0.0s => => transferring context: 8.16kB 0.0s => CACHED [2/4] WORKDIR /app 0.0s => CACHED [3/4] COPY . . 0.0s => CACHED [4/4] RUN yarn install --production 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => exporting manifest ``` The subsequent build was completed in just 1.0 second by leveraging the cached layers. No need to repeat time-consuming steps like installing dependencies.
Steps | Description | Time Taken(1st Run) | Time Taken (2nd Run) |
1 | Load build definition from Dockerfile | 0.0 seconds | 0.0 seconds |
2 | Load metadata for docker.io/library/node:20-alpine | 2.7 seconds | 0.9 seconds |
3 | Load .dockerignore | 0.0 seconds | 0.0 seconds |
4 | Load build context
(Context size: 4.60MB) |
0.1 seconds | 0.0 seconds |
5 | Set the working directory (WORKDIR) | 0.1 seconds | 0.0 seconds |
6 | Copy the local code into the container | 0.0 seconds | 0.0 seconds |
7 | Run yarn install --production | 10.0 seconds | 0.0 seconds |
8 | Exporting layers | 2.2 seconds | 0.0 seconds |
9 | Exporting the final image | 3.0 seconds | 0.0 seconds |