Sub topic 2
Which Dockerfile instructions add layers and size
This chapter assumes you can build and run images, read a simple Dockerfile, and use a Linux shell. It also assumes you know that an image is a stack of read-only layers. Here you learn which instructions add to that stack and which add only metadata.
Instructions that change the filesystem
Each RUN, COPY, and ADD instruction produces a layer that holds the files it changed. The layer's size is the size of that change, not the size of the whole image. The image size is roughly the sum of all its layers.
Other instructions record configuration and add no files. ENV, WORKDIR, CMD, ENTRYPOINT, EXPOSE, LABEL, and USER write metadata to the image config. With BuildKit they show a size of 0B in docker history.
| Instruction | Adds files to a layer | Typical size |
|---|---|---|
| `FROM` | Brings in the base image's layers | Size of the base image |
| `RUN` | Yes, whatever the command writes | Varies |
| `COPY` / `ADD` | Yes, the copied files | Size of the copied files |
| `ENV`, `CMD`, `EXPOSE`, `LABEL`, `USER` | No, metadata only | 0B |
| `WORKDIR` | Only if the directory is created | 0B or a few bytes |
Why deleting a file does not shrink the image
A later layer cannot remove bytes from an earlier layer. When a RUN rm deletes a file, the union filesystem records a whiteout marker in the new layer. The file is hidden from the running container, but its bytes stay in the earlier layer. The image still carries them.
Consider the wasteful Dockerfile from this tutorial:
FROM debian:bookworm-slim
RUN apt-get update
RUN apt-get install -y curl
COPY data.tar /tmp/data.tar
RUN tar -xf /tmp/data.tar -C /opt
RUN rm /tmp/data.tarIt has three problems:
apt-get updatestores package lists under/var/lib/apt/lists, and nothing removes them.COPY data.tarstores the full tarball in its own layer.RUN rm /tmp/data.taradds a whiteout but cannot reclaim the tarball. The tarball is paid for twice: once in the COPY layer, and again as extracted files in/opt.
Fixing it within the instruction model
Two changes address the waste. Both depend on knowing which instruction owns which bytes.
- Put creation and cleanup in one
RUN. The apt lists are created and removed before the layer is committed, so they never appear in it. - Keep large inputs out of layers. A BuildKit bind mount makes
data.tarvisible to one command without storing it.
FROM debian:bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*
RUN --mount=type=bind,source=data.tar,target=/tmp/data.tar \
tar -xf /tmp/data.tar -C /optThe --no-install-recommends flag also skips optional packages that apt would otherwise add. The extracted files in /opt still take space, but the tarball no longer does.
Common errors
- Cleaning in a separate
RUN.RUN apt-get install ...followed byRUN rm -rf /var/lib/apt/lists/*saves nothing. The lists are already in the earlier layer. - Splitting
apt-get updatefromapt-get install. A cachedupdatelayer can serve stale package lists to a laterinstall. Run them in one instruction. - Using
chmodorchownon large trees in a later layer. Changing metadata on a file copies the whole file into the new layer. UseCOPY --chown=...instead, or set permissions in the same instruction that creates the files. - Assuming fewer layers always means a smaller image. Merging instructions helps only when it lets cleanup happen in the same layer. Merging unrelated steps just weakens the build cache.
Exercise
A Dockerfile has COPY build.zip /tmp/, then RUN unzip /tmp/build.zip -d /app, then RUN rm /tmp/build.zip. The zip is 80 MB. Does the final image shrink by 80 MB compared with leaving out the rm? Why, and what change would actually avoid storing the zip?
Show answer
No. The rm adds a whiteout in a new layer, but the 80 MB stays in the COPY layer, so the image size is unchanged. To avoid storing the zip, mount it with RUN --mount=type=bind,source=build.zip,target=/tmp/build.zip unzip /tmp/build.zip -d /app, or use a multi-stage build that copies only the extracted files into the final stage.