Courseiva
Container Virtualization →mediumMultiple Choice

305-300 Practice Question: Building a Docker image for a Python web application

You are building a Docker image for a Python web application. The Dockerfile currently contains:

FROM python:3.9-slim COPY . /app WORKDIR /app RUN pip install -r requirements.txt CMD ["python", "app.py"]

After building, you notice that every time you change app.py, the entire pip install step re-runs, making builds slow. Which change to the Dockerfile will best address this?

⚠ Common exam trap

The trap here is assuming that any COPY instruction invalidates all subsequent layers, when in fact Docker's cache is keyed to the specific files referenced by each COPY, so ordering matters.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

Move the COPY instruction to after the RUN pip install instruction, and copy only requirements.txt before the RUN.

Docker builds images layer by layer and caches each layer based on the instruction and the files it references. By copying only requirements.txt and running pip install before copying the rest of the source, the expensive dependency installation layer remains cached as long as requirements.txt is unchanged. Subsequent changes to application code only invalidate later layers, resulting in much faster rebuilds.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Add a .dockerignore file that excludes app.py so that changes to it do not invalidate the cache.

    Why it's wrong here

    Excluding app.py via .dockerignore would prevent the application source from being copied into the image at all, breaking the build. The .dockerignore file is meant to exclude unnecessary files like .git or __pycache__ to reduce context size and avoid cache invalidation from irrelevant files, not to exclude the main application code that must be present in the final image.

  • ✗

    Use a multistage build with a separate stage for dependencies, and copy the installed packages from the builder stage into the final stage.

    Why it's wrong here

    A multistage build can reduce final image size by leaving build tools behind, but it does not inherently solve the cache invalidation problem described. Unless the dependency installation is isolated from source code changes, the builder stage will still re-run pip install whenever any copied file changes, so the build remains slow.

  • ✗

    Combine the COPY and RUN instructions into a single RUN instruction using a shell command to copy files and install dependencies.

    Why it's wrong here

    Combining instructions into a single RUN does not help caching; it actually makes the layer less granular. Docker caches at the instruction level, so merging COPY and pip install means any source change invalidates the entire combined layer, causing pip install to re-run. This worsens the problem rather than solving the cache invalidation issue.

  • ✓

    Move the COPY instruction to after the RUN pip install instruction, and copy only requirements.txt before the RUN.

    Why this is correct

    Placing COPY requirements.txt before RUN pip install allows Docker to cache the dependency layer. When only app.py changes, the cached pip install layer is reused, drastically speeding up rebuilds. Copying the rest of the source after the RUN ensures that source changes do not invalidate the dependency installation layer, which is the core optimization for build caching.

About these practice questions

One of 210 original 305-300 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

You might also like

Similar 305-300 questions

Written and reviewed by Johnson Ajibi, MSc IT Security

Security Systems Architect · founder of Courseiva

Last reviewed September 2026 · checked against the official LPI exam blueprint

This 305-300 practice question is part of Courseiva's free LPI certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 305-300 exam.