Oh My Algorithm
Concept GuideUnion FS · Layer Cache

Image Layers

A Docker image isn't one monolithic file — it's read-only layers stacked on top of each other. Each Dockerfile instruction creates one layer, and at run time a writable container layer is added on top so the whole stack looks like a single filesystem. Identical layers are shared between images and cached, which makes instruction order the thing that decides your build speed and image size.

01 Concept at a Glance

Interactive Step-by-Step
Docker Image Layers · Union Filesystem
Image build
✍️5Container layerR/W · copy-on-write
📄4COPY . .app source · 12 MB
📦3RUN npm cinode_modules · 180 MB
📋2COPY package*.jsondependency manifest · 4 KB
🏗1FROM node:20-alpinebase · 140 MB
Image (1 layer)
alpine + node
Base image

A Docker image isn't one monolithic file. It's read-only layers stacked on top of each other, and each Dockerfile instruction creates one layer.

Logic Node1 / 10

02 Understand It Simply

For Everyone
🔑Analogy

Like finishing a drawing on stacked transparency sheets. The bottom sheet (the base image) stays put and you only add new sheets on top; look through them and you see one picture (the filesystem). Swap a sheet in the middle and every sheet above it has to be redrawn.

💡In Plain Words

Dockerfile instructions like FROM, COPY, and RUN each create a read-only layer, and a union filesystem overlays them into one view.

Start a container and a writable layer is added on top — every change is recorded only there (copy-on-write).

Changing a layer invalidates the cache of every layer above it, so the key is to put what rarely changes (installing dependencies) low and what changes constantly (your source) high.

📍Where It's Used
  • Ordering Dockerfile instructions
  • raising build cache hit rates
  • shrinking image size
  • and understanding how multiple images share base layers

03 Frequently Asked Questions

FAQ
What is Image Layers?+

A Docker image isn't one monolithic file — it's read-only layers stacked on top of each other. Each Dockerfile instruction creates one layer, and at run time a writable container layer is added on top so the whole stack looks like a single filesystem. Identical layers are shared between images and cached, which makes instruction order the thing that decides your build speed and image size.

Where is Image Layers used?+

Ordering Dockerfile instructions, raising build cache hit rates, shrinking image size, and understanding how multiple images share base layers.

What's a simple analogy for Image Layers?+

Like finishing a drawing on stacked transparency sheets. The bottom sheet (the base image) stays put and you only add new sheets on top; look through them and you see one picture (the filesystem). Swap a sheet in the middle and every sheet above it has to be redrawn.

Guide Progress0%