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 . .

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.

Further reading

Problem with a question

Node.js · Middle · question 62

Test yourself

Take the Node.js · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz