Home Explore Blog CI



docker

4th chunk of `content/get-started/docker-concepts/building-images/using-the-build-cache.md`
ead534c7053479ce1b525ea07716d9063b1a917e718164650000000100000a41
     COPY package.json yarn.lock ./
     RUN yarn install --production 
     COPY . . 
     EXPOSE 3000
     CMD ["node", "src/index.js"]
     ```

7. Create a file named `.dockerignore` in the same folder as the Dockerfile with the following contents.

     ```plaintext
     node_modules
     ```

8. Build the new image:

    ```console
    $ docker build .
    ```

    You'll then see output similar to the following:

    ```console
    [+] Building 16.1s (10/10) FINISHED
    => [internal] load build definition from Dockerfile                                               0.0s
    => => transferring dockerfile: 175B                                                               0.0s
    => [internal] load .dockerignore                                                                  0.0s
    => => transferring context: 2B                                                                    0.0s
    => [internal] load metadata for docker.io/library/node:21-alpine                                  0.0s
    => [internal] load build context                                                                  0.8s
    => => transferring context: 53.37MB                                                               0.8s
    => [1/5] FROM docker.io/library/node:21-alpine                                                    0.0s
    => CACHED [2/5] WORKDIR /app                                                                      0.0s
    => [3/5] COPY package.json yarn.lock ./                                                           0.2s
    => [4/5] RUN yarn install --production                                                           14.0s
    => [5/5] COPY . .                                                                                 0.5s
    => exporting to image                                                                             0.6s
    => => exporting layers                                                                            0.6s
    => => writing image     
    sha256:d6f819013566c54c50124ed94d5e66c452325327217f4f04399b45f94e37d25        0.0s
    => => naming to docker.io/library/node-app:2.0                                                 0.0s
    ```

    You'll see that all layers were rebuilt. Perfectly fine since you changed the Dockerfile quite a bit.

9. Now, make a change to the `src/static/index.html` file (like change the title to say "The Awesome Todo App").

10. Build the Docker image. This time, your output should look a little different.

    ```console
    $ docker build -t node-app:3.0 .
    ```

    You'll then see output similar to the following:

Title: Building and Rebuilding the Docker Image with Caching
Summary
This section details the process of building a Docker image after optimizing the Dockerfile and creating a '.dockerignore' file. It presents the console output of the first build, where all layers are rebuilt due to significant changes in the Dockerfile. Subsequently, it instructs the user to modify a file (src/static/index.html) and rebuild the image, highlighting that the subsequent build will leverage caching, resulting in a faster build time as only the modified layers will be rebuilt.