Difficulty: Intermediate
How do you optimize a Dockerfile for a Node.js application to speed up builds?
The most important trick is **Layer Caching**. You should copy `package.json` and run `npm install` *before* copying the rest of your source code. This way, Docker only re-runs `npm install` if your dependencies change, not every time you edit a line of code.
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "index.js"]
If only source files changed, Docker skips 'npm install' and uses the cached layer, saving minutes.
Layer Caching, Node.js, Optimization