Docker/Image/Layer caching

Aus Foxwiki
Version vom 20. November 2026, 11:19 Uhr von Dirkwagner (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „== Layer caching == Now that you've seen the layering in action, there's an important lesson to learn to help decrease build times for your container images * Once a layer changes, all downstream layers have to be recreated as well Look at the following Dockerfile you created for the getting started app <syntaxhighlight lang="Dockerfile" line copy> syntax=docker/dockerfile:1 FROM node:lts-alpine WORKDIR /app COPY . RUN yarn install --production CMD ["n…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu:Navigation, Suche

Layer caching

Now that you've seen the layering in action, there's an important lesson to learn to help decrease build times for your container images

  • Once a layer changes, all downstream layers have to be recreated as well

Look at the following Dockerfile you created for the getting started app

syntax=docker/dockerfile:1
FROM node:lts-alpine
WORKDIR /app
COPY . 
RUN yarn install --production
CMD ["node", "src/index.js"]

Going back to the image history output, you see that each command in the Dockerfile becomes a new layer in the image

  • You might remember that when you made a change to the image, the yarn dependencies had to be reinstalled
  • It doesn't make much sense to ship around the same dependencies every time you build
Caching von Abhängigkeiten

To fix it, you need to restructure your Dockerfile to help support the caching of the dependencies

  • For Node-based applications, those dependencies are defined in the package.json file
  • You can copy only that file in first, install the dependencies, and then copy in everything else
  • Then, you only recreate the yarn dependencies if there was a change to the package.json

1. Update the Dockerfile to copy in the package.json first, install dependencies, and then copy everything else in

syntax=docker/dockerfile:1
FROM node:lts-alpine
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --production
COPY . 
CMD ["node", "src/index.js"]

2. Build a new image using docker build

docker build -t getting-started .

You should see output like the following

 [+] Building 16.1s (10/10) FINISHED
 => [internal] load build definition from Dockerfile
 => => transferring dockerfile: 175B
 => [internal] load .dockerignore
 => => transferring context: 2B
 => [internal] load metadata for docker.io/library/node:lts-alpine
 => [internal] load build context
 => => transferring context: 53.37MB
 => [1/5] FROM docker.io/library/node:lts-alpine
 => CACHED [2/5] WORKDIR /app
 => [3/5] COPY package.json yarn.lock ./
 => [4/5] RUN yarn install --production
 => [5/5] COPY . 
 => exporting to image
 => => exporting layers
 => => writing image   sha256:d6f819013566c54c50124ed94d5e66c452325327217f4f04399b45f94e37d25
 => => naming to docker.io/library/getting-started
  • Now, make a change to the src/static/index.html file
  • For example, change the <title> to "The Awesome Todo App"
  • Build the Docker image now using docker build -t getting-started again
  • This time, your output should look a little different
 [+] Building 1.2s (10/10) FINISHED
 => [internal] load build definition from Dockerfile
 => => transferring dockerfile: 37B
 => [internal] load .dockerignore
 => => transferring context: 2B
 => [internal] load metadata for docker.io/library/node:lts-alpine
 => [internal] load build context
 => => transferring context: 450.43kB
 => [1/5] FROM docker.io/library/node:lts-alpine
 => CACHED [2/5] WORKDIR /app
 => CACHED [3/5] COPY package.json yarn.lock ./
 => CACHED [4/5] RUN yarn install --production
 => [5/5] COPY . 
 => exporting to image
 => => exporting layers
 => => writing image   sha256:91790c87bcb096a83c2bd4eb512bc8b134c757cda0bdee4038187f98148e2eda
 => => naming to docker.io/library/getting-started

First off, you should notice that the build was much faster

  • And, you'll see that several steps are using previously cached layers
  • Pushing and pulling this image and updates to it will be much faster as well