DevOps and cloud / Beginner / 5 min / Updated
Docker images: layers and the build cache
Edit code and see which image layers rebuild. Dockerfile order, slim base images and multi-stage builds cut build time and image size.
Start the simulation → Change the load, break a part, watch what happens. Free, no sign-up.
In short
What is docker layers?
A Docker image is a stack of read-only layers, one per Dockerfile line, cached by their inputs and shared between builds and servers.
Why does it matter?
Layers make builds fast and pushes small: only changed layers rebuild, upload and download, so the order of the Dockerfile matters.
What is the trade-off?
Copy rarely-changing files (package.json) before code to keep the cache; slim bases and multi-stage builds shrink images but drop tools.
How do you explain it in an interview?
"Multi-stage build: install deps from the lockfile first for cache hits, build, then copy only the output into a slim or distroless runtime image."
Check yourself
Your Dockerfile copies all source code before RUN npm ci. You edit one line of code. What rebuilds?
Answer: The code layer and every layer after it, including npm ci. A changed layer invalidates every layer after it. Copy package.json and install first, then copy the code.
What does a multi-stage build give you?
Answer: A final image with only the runtime and built output, not compilers and dev dependencies. Build in a full image, then copy only the output into a slim runtime stage. Smaller images pull faster and expose less.
You push a new image after a small code change. How much is uploaded?
Answer: Only the layers the registry does not already have. Layers are content-addressed; unchanged layers are already in the registry and on servers, so only the changed ones move.