Bird
Raised Fist0
Dockerdevops~20 mins

Layer caching and ordering in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Layer Caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Effect of Dockerfile instruction order on cache usage

Consider this Dockerfile snippet:

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

If you change the order of the COPY instructions to copy the entire app directory before copying requirements.txt, what will happen when you rebuild the image after only changing app.py?

Docker
FROM python:3.12-slim
COPY requirements.txt /app/
RUN pip install -r /app/requirements.txt
COPY . /app/
CMD ["python", "/app/app.py"]
ADocker will reuse the cached layer for pip install because requirements.txt did not change.
BDocker will fail to build because of conflicting COPY instructions.
CDocker will skip copying app.py because it was not changed.
DDocker will rerun pip install because copying the entire app directory first invalidates the cache for requirements.txt.
Attempts:
2 left
💡 Hint

Think about how Docker caches layers based on the exact content of files copied before a RUN command.

🧠 Conceptual
intermediate
1:30remaining
Why is ordering Dockerfile instructions important for caching?

Which statement best explains why ordering instructions in a Dockerfile affects build speed?

ADocker caches each layer and reuses it only if the instruction and its context have not changed, so ordering stable instructions first maximizes cache reuse.
BDocker executes instructions in parallel, so order does not affect caching.
CDocker ignores file changes when caching layers, so ordering only affects readability.
DDocker rebuilds all layers every time regardless of instruction order.
Attempts:
2 left
💡 Hint

Think about what triggers cache invalidation in Docker builds.

❓ Troubleshoot
advanced
2:00remaining
Diagnosing unexpected cache invalidation in Docker builds

You notice that your Docker build always reruns RUN npm install even though package.json has not changed. Your Dockerfile snippet is:

FROM node:20
COPY . /app
WORKDIR /app
RUN npm install
CMD ["node", "server.js"]

What is the most likely cause?

Docker
FROM node:20
COPY . /app
WORKDIR /app
RUN npm install
CMD ["node", "server.js"]
ACopying the entire directory before <code>RUN npm install</code> causes cache invalidation if any file changes, even unrelated ones.
BThe <code>npm install</code> command always runs and is never cached.
CThe <code>WORKDIR</code> instruction invalidates the cache for <code>RUN npm install</code>.
DThe <code>CMD</code> instruction causes cache invalidation for previous layers.
Attempts:
2 left
💡 Hint

Consider what files affect the cache for the RUN npm install layer.

🔀 Workflow
advanced
2:30remaining
Optimizing Dockerfile for faster rebuilds with frequent code changes

You have a Python app with dependencies listed in requirements.txt. You frequently change app.py but rarely change dependencies. Which Dockerfile instruction order best optimizes build caching?

A
COPY . /app
RUN pip install -r /app/requirements.txt
CMD ["python", "/app/app.py"]
B
COPY requirements.txt /app/
RUN pip install -r /app/requirements.txt
COPY . /app/
CMD ["python", "/app/app.py"]
C
RUN pip install -r /app/requirements.txt
COPY . /app/
CMD ["python", "/app/app.py"]
D
COPY app.py /app/
COPY requirements.txt /app/
RUN pip install -r /app/requirements.txt
CMD ["python", "/app/app.py"]
Attempts:
2 left
💡 Hint

Think about which files change often and which do not, and how Docker caches layers.

✅ Best Practice
expert
3:00remaining
Best practice for minimizing Docker image rebuild time with frequent source code changes

Which Dockerfile layering strategy best minimizes rebuild time when source code changes frequently but dependencies rarely change?

ACombine all COPY commands into one to reduce layers and copy everything at once.
BCopy source code first, then dependency files, then install dependencies to ensure latest code is always used.
CCopy dependency files first, run dependency installation, then copy source code separately to maximize cache reuse of dependencies.
DRun dependency installation before any COPY commands to avoid copying unnecessary files.
Attempts:
2 left
💡 Hint

Consider how Docker cache invalidation works with file changes and layer order.

Practice

(1/5)
1. What is the main benefit of ordering instructions properly in a Dockerfile regarding layer caching?
easy
A. It allows running containers faster
B. It reduces the final image size automatically
C. It makes the Dockerfile easier to read
D. It speeds up rebuilds by reusing cached layers

Solution

  1. Step 1: Understand Docker layer caching

    Docker caches each build step as a layer. If a step and all previous steps are unchanged, Docker reuses the cached layer.
  2. Step 2: Effect of instruction order on caching

    Placing stable instructions early means those layers rarely change and get reused, speeding up rebuilds.
  3. Final Answer:

    It speeds up rebuilds by reusing cached layers -> Option D
  4. Quick Check:

    Layer caching = faster rebuilds [OK]
Hint: Put stable steps first to reuse cache and speed builds [OK]
Common Mistakes:
  • Thinking order reduces image size automatically
  • Believing order affects container runtime speed
  • Confusing readability with caching benefits
2. Which of the following Dockerfile instructions is best placed near the top to maximize cache reuse?
easy
A. RUN apt-get update && apt-get install -y curl
B. ENV APP_ENV=production
C. COPY ./app /app
D. RUN pip install -r requirements.txt

Solution

  1. Step 1: Identify stable vs frequently changing instructions

    ENV instructions usually change less often than copying app code or installing packages.
  2. Step 2: Place stable instructions early

    Placing ENV early helps cache it and subsequent layers, avoiding rebuilds when code changes.
  3. Final Answer:

    ENV APP_ENV=production -> Option B
  4. Quick Check:

    Stable instructions early = better cache [OK]
Hint: Put environment variables before copying code [OK]
Common Mistakes:
  • Putting COPY before ENV causing cache bust
  • Installing packages after code copy
  • Assuming RUN commands never change
3. Given this Dockerfile snippet, what happens if you change only app.py and rebuild?
FROM python:3.12
COPY requirements.txt /app/
RUN pip install -r /app/requirements.txt
COPY app.py /app/
CMD ["python", "/app/app.py"]
medium
A. Only the last COPY and CMD steps are rebuilt; earlier layers are cached
B. All layers are rebuilt from the first COPY instruction
C. The RUN step is rebuilt but COPY steps are cached
D. The entire image is rebuilt ignoring cache

Solution

  1. Step 1: Identify which layers change

    Changing app.py affects only the COPY app.py step and later steps.
  2. Step 2: Understand Docker caching behavior

    Layers before COPY app.py remain unchanged and cached, so only last COPY and CMD rebuild.
  3. Final Answer:

    Only the last COPY and CMD steps are rebuilt; earlier layers are cached -> Option A
  4. Quick Check:

    Change in later file = rebuild later layers only [OK]
Hint: Change late files to rebuild fewer layers [OK]
Common Mistakes:
  • Assuming all layers rebuild on any file change
  • Thinking RUN step rebuilds when app.py changes
  • Believing CMD step never rebuilds
4. You have this Dockerfile:
FROM node:20
COPY . /app
RUN npm install
CMD ["node", "/app/index.js"]

After changing index.js, the npm install step runs again. Why?
medium
A. Because COPY . /app invalidates cache for all later steps
B. Because package.json was changed
C. Because npm install always runs
D. Because index.js is copied before npm install

Solution

  1. Step 1: Analyze Dockerfile order and cache invalidation

    COPY . /app copies all files including index.js before npm install step.
  2. Step 2: Effect of changing index.js on cache

    Changing index.js changes the context for COPY . /app, invalidating cache for that step and all following steps, including npm install.
  3. Final Answer:

    Because COPY . /app invalidates cache for all later steps -> Option A
  4. Quick Check:

    Later COPY changes bust cache for previous RUN [OK]
Hint: Copy package files before code to cache npm install [OK]
Common Mistakes:
  • Thinking npm install always runs
  • Assuming index.js change affects package.json
  • Believing COPY . /app is placed after npm install
5. You want to optimize this Dockerfile for faster rebuilds when source code changes:
FROM python:3.12
COPY . /app
RUN pip install -r /app/requirements.txt
CMD ["python", "/app/app.py"]

What is the best change to improve layer caching?
hard
A. Add ENV variables before COPY
B. Move CMD before RUN to cache install step
C. Copy only requirements.txt first, run pip install, then copy rest of source code
D. Combine COPY and RUN into one step

Solution

  1. Step 1: Identify frequent changes and stable files

    Source code changes often, requirements.txt changes less frequently.
  2. Step 2: Reorder Dockerfile for caching

    Copy requirements.txt and run pip install first to cache dependencies, then copy source code to avoid reinstalling on code changes.
  3. Final Answer:

    Copy only requirements.txt first, run pip install, then copy rest of source code -> Option C
  4. Quick Check:

    Separate dependencies copy for caching [OK]
Hint: Copy dependencies first, then source code [OK]
Common Mistakes:
  • Moving CMD before RUN (invalid order)
  • Combining COPY and RUN loses cache benefits
  • Adding ENV doesn't affect caching of installs
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard