Cloud and Container Basics
FROM node:lts
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
CMD ["node", "dist/server.js"]Dockerfile (B) above was made from Dockerfile (A), which had RUN npm ci after COPY . ., by changing the order so that only the dependency files are copied first. npm ci takes 120 seconds, npm run build 30 seconds and COPY . . 2 seconds, and the time of the other instructions is negligible. The image is rebuilt 20 times a day after changing only the source code under src (package.json and package-lock.json are unchanged). When the cache from the previous build can be used, which of the following is how much shorter B is than A in total build time per day?
- A2 minutes
- BAbout 11 minutes
- C40 minutes
Show answer and explanation
Answer: C. 40 minutes
The build cache is used instruction by instruction, and for COPY it depends on whether the contents of the copied files are the same as last time. Once the cache cannot be used for an instruction, every instruction after it is executed again. In A, the change is found at the first COPY . ., so every build takes 2 + 120 + 30 = 152 seconds. In B, package.json and package-lock.json are unchanged, so the cache is used up to npm ci, and only COPY . . and npm run build run, taking 2 + 30 = 32 seconds. The difference is 120 seconds per build, or 2,400 seconds = 40 minutes for 20 builds.
- A. This is the difference for one build (120 seconds = 2 minutes). The image is rebuilt 20 times a day, so multiply by 20.
- B. This is B's daily total (32 seconds × 20 = 640 seconds, about 11 minutes), not the difference from A.
- C. Correct. (152 − 32) × 20 = 2,400 seconds = 40 minutes.