What Is a Docker Image Layer?
Learn what a Docker image layer is, how build caching reuses unchanged layers, and how Dockerfile instruction order affects speed and image size.
Expected Interview Answer
A Docker image layer is a read-only, content-addressable snapshot of filesystem changes produced by a single Dockerfile instruction, and an image is simply an ordered stack of these layers merged together through a union filesystem.
Each instruction like RUN, COPY, or ADD in a Dockerfile creates a new layer capturing only the files that changed relative to the previous layer, not the whole filesystem again. Docker caches layers by their content hash, so if an earlier instruction and its inputs haven't changed, Docker reuses the cached layer instead of rebuilding it, which speeds up builds significantly. Multiple images can also share identical layers on disk and during registry transfers, since a layer with the same hash only needs to be stored or downloaded once.
- Rebuilds reuse unchanged layers, making builds much faster
- Shared layers across images save disk space and bandwidth
- Only changed layers need to be pushed or pulled
- Encourages ordering Dockerfile instructions for better cache hits
- Provides a clear, inspectable history of filesystem changes
AI Mentor Explanation
A Docker image layer is like one stroke added to an innings scorecard, where each delivery only records the runs scored since the last ball rather than rewriting the scorecard from over one. If the pitch report and team sheet from an earlier over haven't changed, the scorer reuses that entry instead of noting it again. The scorecard is simply the full stack of these entries read top to bottom.
Step-by-Step Explanation
Step 1
Instruction runs
Each Dockerfile instruction such as RUN or COPY executes against the current filesystem state.
Step 2
Diff is captured
Docker records only the files added, changed, or deleted by that instruction as a new layer.
Step 3
Layer is hashed
The layer's content is hashed, making it addressable and reusable across images with identical content.
Step 4
Cache is checked on rebuild
If the instruction and its inputs are unchanged, Docker reuses the cached layer instead of re-executing it.
Step 5
Union filesystem merges layers
At runtime, all layers are stacked and merged into a single view the container sees as its filesystem.
What Interviewer Expects
- Explains that each Dockerfile instruction produces one layer
- Understands layer caching and how it speeds up rebuilds
- Knows layers are content-addressable and can be shared across images
- Can explain how instruction order affects cache invalidation
- Mentions the union/overlay filesystem that merges layers at runtime
Common Mistakes
- Believing every layer duplicates the entire filesystem
- Placing frequently changing instructions like COPY before stable ones like package installs
- Assuming layers are always private to a single image
- Confusing a layer with the final flattened container filesystem
Best Answer (HR Friendly)
“A Docker image layer is like one saved step in building an application environment, where only the changes from that step are stored instead of the whole environment each time. Stacking these steps together builds the final application image, and reusing unchanged steps makes rebuilding much faster.”
Code Example
FROM node:20-slim # base layer
WORKDIR /app # metadata layer
COPY package*.json ./ # layer: only package files
RUN npm ci # layer: installed node_modules
COPY . . # layer: rest of app source
CMD ["node", "server.js"]
# Ordering COPY package*.json before COPY . . means
# `npm ci` layer is cached and skipped unless dependencies change.Follow-up Questions
- How does layer caching speed up Docker builds?
- Why should you order Dockerfile instructions from least to most frequently changing?
- How do multiple images share the same layer on disk?
- What is the difference between a layer and the final container filesystem?
- How does docker history show an image's layers?
MCQ Practice
1. What does each Dockerfile instruction typically produce?
Each instruction like RUN or COPY produces a new layer recording only the changes since the previous layer.
2. What allows Docker to skip re-running an unchanged instruction during a rebuild?
Docker hashes layer inputs and reuses a cached layer when the instruction and its inputs haven't changed.
3. Why can two different images share disk space for a common base layer?
Layers are identified by content hash, so identical layers across images are stored and transferred only once.
Flash Cards
What is a Docker image layer? — A read-only snapshot of filesystem changes produced by one Dockerfile instruction.
Why does instruction order matter in a Dockerfile? — Changing an early instruction invalidates the cache for every layer after it.
How are layers stored efficiently? — They are content-addressable, so identical layers are stored and shared only once.
What merges all layers into one filesystem view? — A union/overlay filesystem at container runtime.