Appearance
Images, Containers and Layers
21. What is a Docker image?
The short answer
An image is a read-only package that holds everything needed to start a container. It has two parts: a stack of filesystem layers with all the files the app needs, and a small configuration that says how to start it, such as the command, environment variables and user. Everything in it is identified by a cryptographic hash, so an image can never change without its identity changing too.
Think of an image as a frozen filesystem plus a note that says how to start it.
The frozen filesystem is every file the app needs: a minimal operating system's files, a language runtime, your libraries and your code. The note is a small piece of configuration: "run node server.js as user node, in the folder /app, with PORT=8080."
Under the surface, an image is three kinds of object:
- Layers. Each layer is a compressed archive of files. Stacked in order, they make up the full filesystem. Question 26 explains layers.
- The config. A JSON document holding the start command, environment variables, working directory, user, exposed ports, CPU type, and the ordered list of layers.
- The manifest. A tiny JSON document that lists the config and the layers by their hash and size. The manifest is what ties the image together.
Every one of these objects is named by its digest. A digest is the SHA-256 hash of the object's bytes, written as sha256: followed by 64 hexadecimal characters. A hash is a fingerprint: change one byte of the content and the fingerprint changes completely. So the name of an object proves what is inside it.
You can see the config part of any image:
bash
docker image inspect node:20-slim --format '{{json .Config}}' # (1)json
{
"Env": ["PATH=/usr/local/sbin:/usr/local/bin:...", "NODE_VERSION=20.18.0"],
"Cmd": ["node"],
"WorkingDir": "",
"User": ""
}(1) asks Docker to print just the Config section of the image as JSON. The output shows the default environment and the default command, node. Nothing here is a file. It is the "how to start" note.
An image does nothing by itself. It is like a program file sitting on a disk. It becomes something that runs only when you create a container from it, which is question 22.
22. What is a Docker container?
The short answer
A container is a running instance of an image. It is an ordinary Linux process that the kernel isolates: it sees only its own files, processes and network, and it can use only the CPU and memory it is allowed. On top of the image's read-only layers, each container gets its own thin writable layer for any changes it makes.
When you create a container, Docker takes an image and adds the things a running program needs:
- A writable layer on top of the image's read-only layers. Any file the container creates or changes goes here. Question 27 explains it.
- Namespaces, which give the process its own private view: its own process list, its own network with its own IP address, its own hostname, and its own filesystem root. Question 99 covers each kind.
- A cgroup, which caps how much memory and CPU it may use. Question 100 covers cgroups.
- Its settings for this run: environment variables, port mappings, mounted volumes, a restart policy, and a name.
Then it starts the image's command as a process inside all of that.
A container is not always running. It has a life cycle, and you can see the state with docker ps -a:
| State | Meaning |
|---|---|
| created | Set up, process not started yet |
| running | Process is alive |
| paused | Process frozen in place |
| restarting | Being restarted by its policy |
| exited | Process ended; files kept |
| dead | Removal failed partway |
An exited container still exists. Its writable layer, logs and settings are kept until you remove it with docker rm. That is why you can read the logs of a container that crashed an hour ago.
The key idea: a container is a process. If you look at the host with ps, you will see your app's process right there among the host's other processes. The isolation is a set of walls the kernel builds around it, not a separate machine.
23. How are an image and a container different?
The short answer
An image is a read-only template that sits on disk and never changes. A container is a live instance made from it, with its own writable layer, its own process and its own settings. The relationship is the same as between a program file and a running program: one program file, many running copies.
The cleanest comparison is a program on your computer. The file chrome.exe sits on the disk and never changes. When you start Chrome, the computer creates a running process from that file, with its own memory and its own state. Start Chrome twice and you get two processes from one file.
The image is the file. The container is the running process.
Programmers sometimes use another comparison: an image is like a class, and a container is like an object created from that class.
| Image | Container | |
|---|---|---|
| What it is | Template | Running instance |
| Changes? | Never, read-only | Writable layer changes |
| Has a process? | No | Yes, while running |
| Where it lives | Disk and registries | One host |
| Identity | Digest of its content | Container ID and name |
| Created by | docker build | docker create or docker run |
| Removed by | docker rmi | docker rm |
One practical consequence. If you change something inside a running container, such as installing a package by hand, the image does not change. Delete the container and the change is gone. To change what every container gets, you change the Dockerfile and build a new image. This is the habit of immutable infrastructure: never patch what is running, replace it with a new build.
24. Can multiple containers be created from one image, and what does each container share or isolate?
The short answer
Yes. One image can back any number of containers. They all share the image's read-only layers, which are stored only once on disk. Each container gets its own writable layer, its own processes, its own network and IP address, its own hostname, its own settings and its own resource limits. What they all still share is the host's kernel.
Say you run three copies of the shop's API from one image:
bash
docker run -d --name api-1 -p 8081:8080 checkout-api:1.4 # (1)
docker run -d --name api-2 -p 8082:8080 checkout-api:1.4
docker run -d --name api-3 -p 8083:8080 checkout-api:1.4(1) starts a container in the background (-d), names it api-1, and connects the host's port 8081 to the container's port 8080. The other two lines do the same with different names and host ports. Inside, all three listen on port 8080. They do not clash, because each container has its own network namespace and therefore its own port 8080.
Shared between the containers:
- The image's read-only layers. Stored once on disk, no matter how many containers use them.
- The kernel's cached copy of those files. When one container reads a library file, the kernel keeps it in memory, and the others reuse that copy.
- The host kernel itself. Every container makes its system calls to the same kernel.
Private to each container:
- Its writable layer. A file
api-1writes is invisible toapi-2. - Its process tree. Each container sees its own app as process number 1, and cannot see the others' processes.
- Its network. Each gets its own IP address, its own ports and its own routing.
- Its hostname, its mount table, and its shared-memory area.
- Its settings. Environment variables, mounts and limits are chosen per container at run time. You can run the same image with
LOG_LEVEL=debugin one andLOG_LEVEL=infoin another. - Its resource limits. Each has its own cgroup.
One thing becomes shared only if you choose it: a volume. If you mount the same volume into two containers, both read and write the same files. That is useful for sharing data, and dangerous if both write the same file at once.
25. What is a Dockerfile?
The short answer
A Dockerfile is a plain text recipe for building an image. Each line is one instruction, such as "start from this base image", "copy these files", "run this command" or "start the app like this". Docker runs the instructions in order, and the result is an image. Because it is plain text, it lives in Git next to the code and is reviewed like code.
Here is a small, realistic Dockerfile for the shop's TypeScript API:
dockerfile
# (1)
FROM node:20-slim
# (2)
WORKDIR /app
# (3)
COPY package.json package-lock.json ./
# (4)
RUN npm ci --omit=dev
# (5)
COPY dist/ ./dist/
# (6)
ENV PORT=8080
# (7)
USER node
# (8)
EXPOSE 8080
# (9)
CMD ["node", "dist/server.js"]Walk it line by line:
FROMpicks the starting image. Here it is an official image with Node.js 20 on a small Debian system. Everything else is built on top of it.WORKDIRsets the folder where the next commands run, and creates it if needed.COPYcopies the two dependency files from your project into the image. They are copied before the code on purpose. Question 63 explains why this makes builds much faster.RUNexecutes a command during the build.npm ciinstalls exactly the versions in the lock file, and--omit=devskips development-only tools.- Copies the compiled application code.
ENVsets an environment variable that every container from this image will have.USERmakes the app run as the unprivileged usernodeinstead of root. Question 139 explains why that matters.EXPOSErecords that the app listens on port 8080. It is documentation only. Question 58 explains this common surprise.CMDsets the command a container runs when it starts.
You build it with:
bash
docker build -t checkout-api:1.4 . # (1)(1) builds an image from the Dockerfile in the current folder (.) and tags it checkout-api:1.4. The . is also the build context, the set of files the build is allowed to copy from. Question 59 covers the build context.
Why a recipe instead of a snapshot? Because you can read it, review it, and rebuild it. If a security patch arrives for the base image, you rebuild from the same recipe and get a patched image. A hand-made snapshot can never be reproduced this way.
26. What is a Docker image layer?
The short answer
A layer is a compressed archive of the filesystem changes made by one build step, such as the files one RUN installed or one COPY added. An image is a stack of layers. Each layer is identified by the hash of its content, so identical layers are stored, cached and downloaded only once, even when many images use them.
A layer is not a full copy of the filesystem. It only holds what one step changed: files added, files modified, and markers for files deleted.
Take this Dockerfile:
dockerfile
# (1)
FROM debian:12-slim
# (2)
RUN apt-get update && apt-get install -y curl \
&& rm -rf /var/lib/apt/lists/*
# (3)
COPY app.sh /usr/local/bin/app.sh
# (4)
CMD ["/usr/local/bin/app.sh"]- The base image brings its own layer: the whole minimal Debian filesystem, about 28 MB compressed.
- This step's layer contains only what
apt-getwrote: thecurlprogram, the libraries it needed, and an updated package database. It does not contain/bin/shagain. That file came from layer 1 and did not change. - This layer holds one small file.
CMDchanges only the image's config. It adds no files, so it creates no real layer.
You can see the layers and their sizes with docker history:
bash
docker history myapp:1.0IMAGE CREATED BY SIZE
a1b2c3d4e5f6 CMD ["/usr/local/bin/app.sh"] 0B
<missing> COPY app.sh /usr/local/bin/app.sh # buildkit 1.2kB
<missing> RUN /bin/sh -c apt-get update && apt-get install… 9.8MB
<missing> # debian.sh --arch 'amd64' out/ 'bookworm' '@17… 74.8MBThe size column shows the uncompressed size of each layer. Docker prints <missing> for layers that do not have their own image ID on this machine. That is normal. It is not an error.
Why layers are so useful:
- Sharing. Twenty images built on
debian:12-slimshare its layer. The machine stores it once. - Fast downloads. When you pull an image, Docker downloads only the layers you do not already have. A new version of your app that changed only your code downloads only the small top layer.
- Build caching. When you rebuild, Docker reuses every layer whose step and inputs did not change. Question 62 explains how it decides.
27. How are image layers different from a container's writable layer?
The short answer
Image layers are read-only, shared by every container made from the image, and pushed to registries as part of the image. The writable layer belongs to exactly one container, holds every change that container makes while it runs, and is deleted when the container is removed. It is never part of the image.
When a container starts, Docker stacks one more layer on top of the image's layers. This top layer is the only one the container can write to.
| Image layers | Writable layer | |
|---|---|---|
| Can be changed | No | Yes |
| Belongs to | The image | One container |
| Shared | By all containers | Never |
| Pushed to a registry | Yes | No |
| Deleted when | Image is removed | Container is removed |
| Typical size | Megabytes to gigabytes | Usually tiny |
What lands in the writable layer. Anything the running app writes inside the container's filesystem: log files written to disk, temporary files, caches, and any file of the image it modifies. Question 29 explains how modification works.
Where it lives on the host. With Docker's usual storage driver, it is a folder under /var/lib/docker/overlay2/, one per container. You can see how much each container has written with:
bash
docker ps -s # (1)(1) adds a SIZE column. The first number is the writable layer's size. The number in brackets, marked "virtual", includes the shared image layers underneath.
Two practical rules follow from this:
- Never keep important data in the writable layer. It disappears with the container. A database's files must go in a volume. Question 91 compares the options.
- Avoid heavy writing in the writable layer. Every first write to an image file costs a full copy of that file, and the layered filesystem is slower than a plain disk for heavy input and output. Volumes skip that layering entirely.
28. Why are Docker images described as layered and mostly immutable?
The short answer
Layered, because an image is a stack of separate layers, each holding one step's changes, so layers can be shared, cached and downloaded independently. Immutable, because each layer and the image itself are named by the hash of their content, so they cannot change without becoming something with a different name. "Mostly" because tags can be moved, and running containers do change, but only in their own writable layer.
Why layered. Splitting an image into layers gives three big savings:
- Storage. Shared base layers are stored once per machine and once per registry.
- Network. Pulling a new version downloads only the layers that changed. Pushing does the same in reverse.
- Build time. Unchanged steps reuse their cached layers instead of running again.
Why immutable. The key is content addressing. A layer's name is the SHA-256 hash of its bytes. The image's manifest lists those hashes, and the image's own identity, its digest, is the hash of the manifest. So:
- Change one byte in one layer, and that layer's hash changes.
- That changes the manifest, so the image digest changes too.
- So an image with a given digest can only ever contain exactly the same bytes.
That property is what makes images safe to share. A hundred containers can use the same layer because nobody can change it. A digest you tested last week is guaranteed to be the same image today. A tampered image cannot pretend to be the original, because its hash would not match.
Why "mostly". Three things do change:
- Tags. A tag such as
checkout-api:latestis a movable label that points at a digest. Someone can push a new image and move the label. Question 35 covers tags versus digests, and question 87 covers whylatestis risky. - Containers. A running container writes to its own writable layer. The image underneath stays untouched.
- Deletion. You can delete images from a machine or a registry. Deletion is not change: the bytes are gone, not altered.
29. What happens when a container writes to a file that was part of the image?
The short answer
The image's copy is never touched. On the first write, the storage driver copies the whole file up from the read-only image layer into the container's writable layer, and the change is made to that copy. From then on the container sees its own copy. This is called copy-on-write. Other containers from the same image still see the original file.
The figure shows the three moments of a write, using the file /etc/app.conf that came from the image.
Step by step:
- Before any write, the file exists only in an image layer. When the container reads it, the read goes straight to the image layer.
- On the first write, the storage driver copies the entire file into the container's writable layer. This is called a copy-up. Then it applies the change to that copy.
- After that, the writable layer's copy hides the original. Every read and write by this container uses the copy. The image layer still holds the original, so every other container from the same image sees the unchanged file.
Deleting a file works in a similar way. The image layer cannot be changed, so the file cannot really be removed. Instead, the driver puts a special marker called a whiteout in the writable layer. The marker says "pretend this file does not exist". The container no longer sees the file, but its bytes are still in the image layer underneath.
Why this matters in real life:
- The copy-up copies the whole file. Changing one byte of a 2 GB file first copies all 2 GB. That is why databases and other heavy writers must use volumes, which skip this machinery. Question 91 covers volumes.
- Container changes are temporary. Remove the container and its writable layer, with all the changed copies, is gone.
- The same mechanism makes images bigger during builds. Deleting a file in a later build step only adds a whiteout. The bytes stay in the earlier layer. Question 73 explains this trap.
Question 463 shows the Linux feature that does all of this, OverlayFS.
463. What is OverlayFS, and how does it make image layers look like one filesystem?
The short answer
OverlayFS is a Linux kernel feature that stacks several folders and shows them as one merged folder. The image layers are the read-only lower folders, the container's writable layer is the upper folder, and the merged view is what the container sees as its root filesystem. When it looks up a file, it checks the upper folder first and then each lower one in order, and the first match wins.
Docker's default storage driver on Linux is called overlay2, and it is built on OverlayFS. An overlay mount needs four folders:
lowerdir: one or more read-only folders, stacked in order. These are the image layers.upperdir: one writable folder. This is the container's writable layer.workdir: a scratch folder OverlayFS uses internally to make changes safely. You never touch it.merged: the combined view. This becomes the container's/.
b exists in three folders, but the container sees only the top one, the changed copy b'. The file a exists only in the base layer, so it shows through from there.How a lookup works. When the container opens a file, OverlayFS searches the folders from the top down: upper first, then each lower folder in order. The first folder that has the file wins. In the figure, b exists in three places, but the container sees only the changed copy b' in the upper folder.
How a write works. Writes always go to the upper folder. If the file lives only in a lower folder, OverlayFS first copies it up, which is the copy-up from question 29.
How a delete works. OverlayFS creates a whiteout in the upper folder. A whiteout is a special empty file, a "character device" with number 0/0, whose only job is to hide the file with the same name below it. Deleting a whole folder can use an opaque marker, which hides everything below that folder at once.
You can see the real folders behind any container. The GraphDriver.Data part of docker inspect lists them:
bash
docker inspect checkout-api-1 --format '{{json .GraphDriver.Data}}'json
{
"LowerDir": "/var/lib/docker/overlay2/9f1e.../diff:/var/lib/docker/overlay2/77ab.../diff",
"UpperDir": "/var/lib/docker/overlay2/c4d2.../diff",
"WorkDir": "/var/lib/docker/overlay2/c4d2.../work",
"MergedDir": "/var/lib/docker/overlay2/c4d2.../merged"
}LowerDir lists the image layers, separated by colons. UpperDir is this container's writable layer. Look inside it on the host and you will find exactly the files this container has created or changed, and nothing else.
Why they ask: it shows you know the mechanism under "copy-on-write" and "layers", which is what lets you reason about image size, write performance and disk usage instead of guessing.