Sub topic 1
Images and Containers: What a Build Produces
Prerequisites
This chapter assumes you can do the following:
- Run
docker build,docker run, anddocker pullfrom the command line. - Read a simple Dockerfile, meaning you know what
FROM,COPY,RUN, andCMDdo at a basic level. - Use a terminal and read its output.
You do not need to know how layers are stored. That is what the rest of this tutorial covers. This chapter sets up the vocabulary the later chapters use.
The build as a pipeline
A Dockerfile is a recipe. docker build follows it and produces an image. docker run then starts a container from that image. Three things are involved, and people often mix them up:
| Thing | What it is | Changes after creation? |
|---|---|---|
| Dockerfile | A text file with build instructions | Yes, you edit it |
| Image | A packaged filesystem plus configuration, built from the Dockerfile | No, it is immutable |
| Container | A running (or stopped) process started from an image, with its own writable layer | Yes, while it runs |
The image is the output of the build. It is a template. You can start many containers from one image, and none of them changes the image.
What an image contains
An image holds two kinds of information:
- Filesystem contents. These are the files your program needs at run time: the base operating system files, installed packages, your application code, and so on. They are stored as a stack of layers. The next chapter looks at layers in detail.
- Configuration metadata. This is a JSON document that records the default command, the entrypoint, environment variables, the working directory, exposed ports, the user, labels, and the history of how the image was built.
An image does not contain a running process or a kernel. A container shares the host's kernel. The image only supplies the files and the settings that tell Docker how to start the process.
You can look at the configuration of an image you already have:
docker pull node:20-slim
docker image inspect --format '{{json .Config}}' node:20-slimThe output shows Env, Cmd, WorkingDir, and other fields. These are the defaults a container gets unless you override them with docker run flags.
Stacked layers, in outline
Each filesystem-changing step in a build adds a read-only layer. A layer stores only the difference from the layers below it: files added, changed, or deleted. The image is the ordered stack of those layers, plus the metadata.
Take this Dockerfile, which is the first example from the plan for this tutorial:
FROM node:20-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
CMD ["node", "dist/server.js"]The build produces roughly this result:
- The layers of
node:20-slim, which come from the base image. - A layer containing the two manifest files copied into
/app. - A layer containing
node_modules, created bynpm ci. - A layer containing the rest of the source code.
- A layer containing the build output, created by
npm run build. - Metadata for
WORKDIRandCMD, which adds configuration but no files.
Which instructions create layers and which only change metadata is the subject of a later chapter. For now, remember that the image is more than one blob. It is a stack, and the build tool treats each element of the stack as a unit it can reuse.
What a container adds
When you run a container, Docker does not copy the image. It mounts the image's read-only layers and adds one thin writable layer on top. Everything the container writes goes to that writable layer:
- Creating a new file puts it in the writable layer.
- Editing a file that came from the image copies the file into the writable layer first, then changes the copy. This is called copy-on-write. A later chapter explains it in depth.
- Deleting a file from the image hides it in the container's view. The file still exists in the read-only layer underneath.
When you remove the container, its writable layer is removed with it. The image is unchanged.
You can see this with a short experiment. Start two containers from the same image, write a file in one, and check the other:
docker run -d --name a debian:bookworm-slim sleep 600
docker run -d --name b debian:bookworm-slim sleep 600
docker exec a sh -c 'echo hello > /tmp/note.txt'
docker exec a cat /tmp/note.txt
docker exec b cat /tmp/note.txt
docker diff a
docker rm -f a bThe first cat prints hello. The second fails with "No such file or directory", because container b has its own writable layer. docker diff a lists the files that container a added or changed compared with the image, which is the content of its writable layer.
Build time versus run time
A common source of confusion is that Dockerfile instructions run at two different moments.
| Moment | Instructions | What happens |
|---|---|---|
| Build time | `RUN`, `COPY`, `ADD`, and others | Commands execute inside temporary build containers. The resulting file changes are saved into layers. |
| Run time | `CMD`, `ENTRYPOINT` | Docker stores them as metadata. They execute only when a container starts. |
For example, RUN npm ci runs once, during the build, and its result (the node_modules directory) is stored in the image. CMD ["node", "dist/server.js"] does not run during the build. It only records what to run later.
This distinction matters for caching. The build cache reuses the results of build-time steps. It never reuses a running process, because the cache works with files and metadata, not with live state.
How the build uses the base image
The FROM instruction picks the starting point. It does not start a build from nothing. It makes the base image's layers the bottom of your stack. If you run docker pull node:20-slim first, or the build pulls it for you, your image reuses those layers exactly. Your own instructions add layers on top.
This has two consequences you will rely on later:
- Two images built from the same base share the base layers. Docker stores them once on disk, and a registry transfers them once.
- If the base image tag points to a new image (for example,
node:20-slimis rebuilt upstream with a security fix), the next build starts from different layers. Every step afterFROMthen runs again, because their inputs have changed.
A tag such as 20-slim is a movable label. It can point to different images over time. Pinning to a digest, which you meet in a later chapter, gives you a fixed reference.
Seeing a build produce an image
Build the Node example and look at the result. This assumes a project with a package.json, a package-lock.json, and a build script.
docker build -t myapp:dev .
docker image ls myapp:dev
docker run --rm myapp:devThe first command runs each instruction and tags the final image myapp:dev. The second shows one image entry, even though the image consists of many layers. The third creates a container, runs node dist/server.js inside it, and deletes the container when it exits because of --rm. The image myapp:dev stays in local storage.
The name myapp:dev is a tag, a human-readable pointer to the image. Docker identifies the image itself by an ID derived from its content. You can have several tags pointing to the same image, and docker image ls will show them as separate rows with the same ID.
Common misunderstandings
- "An image is a snapshot of a running container." Not in a Dockerfile build. A build executes each step in a temporary container and saves the file changes as a layer, then discards the temporary container. Your final image never contained a long-running process.
- "Changing a file inside a container changes the image." It does not. It changes only that container's writable layer. To keep a change, you rebuild the image from a Dockerfile.
- "Stopping a container deletes its data." A stopped container keeps its writable layer until you remove it with
docker rm. Removal is what deletes the data. - "One image means one file blob." An image is a stack of layers plus metadata. This is the foundation for everything in the next chapters: how the cache decides what to rebuild, why deleting files later does not shrink an image, and how registries avoid sending layers twice.
Summary
docker buildturns a Dockerfile into an immutable image.docker runcreates a container from it.- An image is a stack of read-only layers plus configuration metadata.
- A container adds one writable layer. Removing the container removes that layer and leaves the image alone.
RUNandCOPYact at build time.CMDandENTRYPOINTare metadata used at run time.FROMsupplies the bottom layers, and images that share a base share those layers.
Exercise
You start two containers from the same image. In the first container you delete /etc/hostname-backup, a file that exists in the image. Then you run docker rm on that container and start a new container from the image. Is /etc/hostname-backup present in the new container? Explain where the deletion was recorded.
Show answer
Yes, the file is present. The deletion was recorded only in the first container's writable layer, where it hid the file from that container's view. The image's read-only layers were never changed. Removing the container discarded its writable layer, so the new container sees the file again.
Exercise
A Dockerfile ends with RUN npm ci followed by CMD ["node", "dist/server.js"]. Which of the two runs while docker build executes, and which one runs when docker run starts a container? Which of them adds files to the image?
Show answer
RUN npm ci runs during docker build, and its changes to the filesystem (the node_modules directory) are saved as a layer in the image. CMD does not run during the build. It stores the default command as metadata, and Docker runs it when a container starts. Only the RUN step adds files.