Services and User Management

A Dockerfile starts FROM python:3.12-slim, then runs COPY . /app, then RUN pip install -r /app/requirements.txt. Every small source-code edit makes docker build reinstall all dependencies, which is slow. Which change to the Dockerfile fixes this?

Show the answer

Correct answerB. Copy only requirements.txt and run pip install before the COPY of the full source

Each instruction creates a layer, and a changed layer invalidates every layer after it. Copying only requirements.txt first keeps the install layer cached until dependencies change. Moving the install to CMD only delays it to run time and does not speed up builds.