Home Explore Blog CI



docker

2nd chunk of `content/get-started/docker-concepts/building-images/using-the-build-cache.md`
aca60e356d6371752730c1e6bc70e843446e9fd012da83dc0000000100000fa5
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.


    <table>
      <tr>
       <td>Steps
       </td>
       <td>Description
       </td>
       <td>Time Taken(1st Run)
       </td>
       <td>Time Taken (2nd Run)
       </td>
      </tr>
      <tr>
       <td>1
       </td>
       <td>Load build definition from Dockerfile
       </td>
       <td>0.0 seconds
       </td>
       <td>0.0 seconds
       </td>
      </tr>
      <tr>
       <td>2
       </td>
       <td>Load metadata for docker.io/library/node:20-alpine
       </td>
       <td>2.7 seconds
       </td>
       <td>0.9 seconds
       </td>
      </tr>
      <tr>
       <td>3
       </td>
       <td>Load .dockerignore
       </td>
       <td>0.0 seconds
       </td>
       <td>0.0 seconds
       </td>
      </tr>
      <tr>
       <td>4
       </td>
       <td>Load build context
    <p>
    (Context size: 4.60MB)
       </td>
       <td>0.1 seconds

Title: Rebuilding the Application and Observing Cache Usage
Summary
This section guides users through rebuilding the Docker image of a sample application without making any changes. It highlights how Docker's caching mechanism speeds up subsequent builds by reusing cached layers, significantly reducing the build time from 20 seconds in the first run to just 1 second in the subsequent run. A table compares the time taken for each step in the first and second runs, demonstrating the efficiency gained from caching.