Sub topic 1
What an image is and how layers are built
Prerequisites
This chapter assumes you have Docker installed and can run docker run. You should be comfortable with basic command-line use and know what a container is. You do not need to have written a Dockerfile before.
What an image is
An image is a read-only template that contains a filesystem and some metadata. The filesystem holds your application, its libraries, and the operating system files it needs. The metadata records things such as the default command, environment variables, and the CPU architecture.
A container is a running instance of an image. Docker adds a thin writable layer on top of the image when it starts the container. Changes made while the container runs go into that writable layer. The image itself does not change.
Layers
An image is a stack of layers. Each layer stores only the filesystem changes made by one build step, such as files added, modified, or deleted. Docker combines the stack into a single view of the filesystem when a container starts.
Layers are identified by a hash of their content. Because of this, two images that share the same base layers store those layers once on disk. Pulling or pushing an image also transfers only the layers the other side does not already have.
Layers are immutable. A later layer cannot remove data from an earlier layer. It can only hide it. If you add a large file in one step and delete it in the next, the image still carries the file's bytes.
How a build creates layers
When you run docker build, Docker reads a Dockerfile and runs its instructions in order. Instructions that change the filesystem, such as RUN and COPY, each produce a layer. Instructions that only set metadata, such as CMD and ENV, add configuration without adding file data.
Here is a small Dockerfile:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]The instructions map to layers as follows.
| Instruction | Effect |
|---|---|
| `FROM python:3.12-slim` | Starts from the layers of the base image |
| `WORKDIR /app` | Sets the working directory and creates it if needed |
| `COPY requirements.txt .` | Adds a layer containing one file |
| `RUN pip install ...` | Adds a layer containing the installed packages |
| `COPY . .` | Adds a layer containing your application code |
| `CMD [...]` | Records the default command as metadata |
The diagram shows the resulting stack. Each box sits on top of the one before it.
Every layer depends on the one below it. If a step changes, Docker must rebuild that step and every step after it. The next chapters on caching build on this fact, and it is the reason the Dockerfile above copies requirements.txt before the rest of the code.
Common mistakes
- Expecting a delete to shrink the image. Running
rmin a laterRUNdoes not remove data from earlier layers. Remove temporary files in the sameRUNinstruction that created them. - Treating a container's changes as part of the image. Files written in a running container are lost with that container unless you save them elsewhere. They never become part of the image.
- Thinking every instruction adds size. Metadata instructions add almost nothing. Size comes from the file changes in
RUNandCOPY.
Exercise
A Dockerfile has RUN wget https://example.com/big.tar.gz on one line and RUN rm big.tar.gz on the next line. Why is the final image still large, and how would you fix it?
Show answer
Each RUN creates its own layer. The first layer stores the downloaded archive, and the second layer only marks the file as deleted. The archive's bytes remain in the earlier layer. Fix it by doing both steps in one instruction, for example RUN wget https://example.com/big.tar.gz && tar -xzf big.tar.gz && rm big.tar.gz, so the archive is never stored in a layer.
Exercise
Which of these Dockerfile instructions mainly add file data to a new layer: FROM, COPY, CMD, RUN?
Show answer
COPY and RUN add file data. CMD only records metadata. FROM supplies the base layers that already exist in the base image.