Sikau

DevOps and deployment · Day 3 · about 30 min

Layers and the build cache

Order instructions so rebuilds take seconds, not minutes.

Why is every build slow?

Each instruction creates a layer. Docker reuses a cached layer when the instruction and everything before it is unchanged. Copy all your code before npm ci, and changing one line of code reinstalls every package. Copy the package files first, and code changes skip the install.

See the cache work

FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
ENV NODE_ENV=production
EXPOSE 3000
CMD ["node", "server.js"]

The rest of this lesson

  • How the cache decidesexplain
  • Layers also add upexplain
  • Which steps re-run?predict
  • Cache-friendly orderorder
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 14 lessons of DevOps and deployment