Images, Containers, and Layers: How Docker Actually Works
Docker image layers explained: read-only layers, overlay filesystems, the writable container layer, and why deleted files still take up space.

A team’s API image is 1.4 GB. Someone adds RUN rm -rf /tmp/build-cache at the end of the Dockerfile to clean up, rebuilds, and the image is still 1.4 GB. Another developer fixes a config file inside a running container with docker exec, restarts the service, and loses the fix when Compose or a deploy recreates the container. The same mistake surprises both: they picture an image as a disk and a container as a machine.
A Docker image is a template: a list of filesystem layers, each a tarball of file changes, plus configuration such as the default command, environment variables, exposed ports and the working directory. A container is a running (or stopped) instance of an image with its own writable layer, its own process, network and mounts. One image can back any number of containers.
Layers are file diffs
FROM python:3.11-slim # base image layers (Debian + Python)
WORKDIR /app # metadata, tiny layer
COPY requirements.txt . # layer: one file
RUN pip install -r requirements.txt # layer: everything pip installed
COPY . . # layer: your source code
CMD ["python", "app.py"] # metadata only, no layer
Instructions that change the filesystem, mainly RUN, COPY and ADD, produce a new layer containing only the files that changed. Instructions such as CMD, ENV, EXPOSE and LABEL only update the image configuration. You can see the stack, and the size each step added, with:
docker history myapp:latest
docker image inspect myapp:latest --format '{{json .RootFS.Layers}}'
A SHA-256 digest of its contents identifies each layer. Two images that start FROM python:3.11-slim share those base layers byte for byte, so the host stores them once and a registry push or pull skips layers the other side already has.
How layers become one filesystem
On Linux, Docker’s default storage driver, overlay2, uses the kernel’s OverlayFS. It stacks directories:
merged/ <- what the container sees as /
^
upperdir <- the container's writable layer
lowerdir <- image layer N (read-only)
<- image layer N-1 (read-only)
<- ...
<- base layer (read-only)
When the container reads a file, OverlayFS returns the version from the highest layer that has it. When the container writes to a file that exists in a lower layer, OverlayFS first copies the whole file up into the writable layer and modifies the copy. This is copy-on-write. When the container deletes a file from a lower layer, a special “whiteout” entry in the upper layer hides it; the original bytes stay in the image.
Why deleting files does not shrink the image
The same rule applies between image layers. If you add a file in one layer and delete it in a later layer, Docker hides it but does not remove it, so it still ships with every pull:
# WRONG: the 400 MB toolchain is still in the image, just hidden
RUN apt-get update && apt-get install -y build-essential
RUN make release
RUN apt-get purge -y build-essential && rm -rf /var/lib/apt/lists/*
# BETTER: install, build and clean up in one layer
RUN apt-get update \
&& apt-get install -y --no-install-recommends build-essential \
&& make release \
&& apt-get purge -y build-essential \
&& rm -rf /var/lib/apt/lists/*
Cleaning up in the same RUN instruction means the temporary files never enter a committed layer. The best fix for build tools is a multi-stage build, which compiles in one image and copies only the result into a clean final image; multi-stage builds cover that in detail.
The same applies to secrets. A .env file or private key copied in one layer and deleted in the next is still recoverable by anyone who pulls the image and extracts its layers.
Containers: the writable layer
Every new container gets its own empty writable layer. Everything the process writes outside of mounted volumes, such as logs written to files, uploaded files, a SQLite database, or packages installed by hand with docker exec, lands there.
- Stopping a container keeps its writable layer.
docker startbrings it back with the changes intact. - Removing a container with
docker rmdeletes the writable layer permanently. - Recreating a container from a new image, which is what every deployment and every
docker compose upafter a change does, starts from an empty writable layer.
So treat the writable layer as scratch space. Data that must survive belongs in a volume, configuration belongs in the image or the environment, and fixes belong in the Dockerfile. You can see exactly what a container has changed with docker diff <container>, which lists added (A), changed (C) and deleted (D) paths.
Copy-on-write also has a performance cost: writing heavily to large files from lower layers, as a database would, means copying them up first and writing through the overlay. That is another reason databases belong on volumes, which bypass the overlay entirely.
Image IDs, tags and manifests
An image’s identity is its configuration digest; tags are just names pointing at it. When you push, the registry stores a manifest listing the config and layer digests. Many official images ship as a manifest list (or OCI image index) that points to separate manifests for each platform, such as linux/amd64 and linux/arm64. When you pull nginx:1.23, the client picks the manifest matching your machine’s architecture. That is how the same tag works on an Intel server and an Apple Silicon laptop, and why an image you built only for arm64 fails on an amd64 server with an exec format error.
Inspecting images like a pro
| Command | Shows |
|---|---|
docker image ls |
Local images, tags and sizes |
docker history IMAGE |
Each layer, the instruction that created it and its size |
docker image inspect IMAGE |
Config: entrypoint, command, environment, user, layer digests |
docker diff CONTAINER |
Files changed in a container’s writable layer |
docker system df |
Disk used by images, containers, volumes and build cache |
dive IMAGE |
An interactive layer-by-layer file browser that highlights wasted space |
dive is a free open-source tool and the quickest way to find the forgotten cache directory or duplicated node_modules that bloats an image.
Frequently Asked Questions
What is the difference between a Docker image and a container?
An image is a read-only template that bundles filesystem layers and configuration. A container is a running instance of an image, with its own writable layer, process, network and mounts. Many containers can run from the same image.
What is a Docker image layer?
A layer is a set of filesystem changes, stored as a tarball and identified by a content digest. Instructions such as RUN, COPY and ADD create layers. Layers are read-only and shared between images that use the same base.
Why is my Docker image still large after deleting files?
A whiteout entry only hides files deleted in a later layer; they remain in the earlier layer, and Docker still downloads them. Remove temporary files in the same RUN instruction that created them, or use a multi-stage build so build-time files never reach the final image.
What happens to data inside a container when you remove it?
Docker deletes anything in the container’s writable layer along with the container. Stopping a container keeps it, but removing or recreating the container loses it. Store persistent data in volumes or bind mounts.
Layers explain the rest of Docker
Almost every Docker best practice follows from this model. Keep build junk out of committed layers. Order instructions so stable layers come first and get cached. Put data in volumes because the writable layer is disposable. Pin images by digest because tags are just pointers. Picture the stack, and how the engine pulls and stores these layers fits into place. The official documentation is worth keeping open: the Docker documentation on image layers and the OverlayFS storage driver reference. Related reading on this site: writing a Dockerfile and the Docker build cache.
Originally posted on 7 February 2023 and post last modified on 7 February 2023