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?

  1. A2 minutes
  2. BAbout 11 minutes
  3. 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.
Practice this topic Back to topic

Books on Cloud and Container Basics Ad

As an Amazon Associate I earn from qualifying purchases.

More Cloud and Container Basics questions

  1. In Docker, which of the following correctly describes named volumes and bind mounts, used to keep data written by a container?
  2. An image is pulled by specifying registry.example.com/team/web:1.4. Which of the following correctly gives the meaning of each part of this reference?
  3. scratch is an empty image that contains no files. Which of the following correctly describes what the final image built with the multi-stage Dockerfile above contains?
  4. In Kubernetes, which of the following is the smallest unit that is deployed and managed, grouping one or more containers that share the same IP address and volumes?
  5. In Kubernetes, a Deployment with replicas: 3 is running three Pods. Which of the following correctly describes what happens when one of these Pods is deleted with kubectl delete pod?