CS FundamentalsHardProduct-company MCQ pattern

A Dockerfile reads: FROM python:3.12 COPY . /app RUN pip install -r /app/requirements.txt Every small code edit makes the build reinstall all dependencies. What is the best fix?

  1. A. Add --no-cache to every build
  2. B. Copy requirements.txt and run pip install before copying the rest of the code
  3. C. Move RUN pip install above FROM
  4. D. Use a larger base image

Correct answer: B. Copy requirements.txt and run pip install before copying the rest of the code

Docker caches layers in order and invalidates every layer after the first changed one. COPY . changes on any code edit, so the pip layer reruns. Copying only requirements.txt first, then installing, then COPY . keeps the dependency layer cached until requirements change. --no-cache makes it worse.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic