Node.js · Middle · Question 62 of 75
In a Dockerfile for a Node.js app, why copy package.json and the lock file before the source code?
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY . .- 1Because
npm cideletes the source code - ✓Correct answer: So Docker can cache the
npm cilayer and skip reinstalling when only the source changes - 3It's required by the Docker syntax
Explanation
Each instruction is a cached layer. Also run as a non-root user, use a small base image and add a .dockerignore for node_modules.