Sub topic 3
Which Dockerfile instructions create layers
This chapter assumes you can read a simple Dockerfile and have run docker build. It also assumes you know that an image is a stack of read-only layers, as the previous chapter described. Here you will learn which instructions add a layer and which only record metadata.
The rule
Only instructions that change the filesystem produce a layer with content. Instructions that set configuration, such as the default command or an environment variable, are stored in the image's metadata. They add a history entry with a size of 0 bytes.
| Instruction | Adds filesystem layer? | What it does |
|---|---|---|
| `FROM` | Yes (the base image's layers) | Starts from the layers of the base image |
| `RUN` | Yes | Runs a command and stores the files it changed |
| `COPY` | Yes | Copies files from the build context or another stage |
| `ADD` | Yes | Like `COPY`, plus URL download and archive extraction |
| `ENV`, `ARG`, `LABEL` | No | Metadata only |
| `WORKDIR`, `USER` | No (see the note below) | Metadata only |
| `EXPOSE`, `CMD`, `ENTRYPOINT` | No | Metadata only |
A worked example
This Dockerfile has seven instructions. Count which ones add content.
FROM debian:bookworm-slim
ENV APP_ENV=production
WORKDIR /app
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*
COPY app.sh /app/app.sh
EXPOSE 8080
CMD ["/app/app.sh"]The base image supplies its own layers. RUN adds one layer holding the installed curl files. COPY adds one layer holding app.sh. ENV, EXPOSE, and CMD change only the image configuration. Building and running docker history on this image shows 0B next to each of those metadata steps.
One RUN, one layer
Each RUN creates its own layer, no matter how small the change is. Chaining commands with && puts them in a single layer. This matters for two reasons:
- Fewer layers mean fewer steps to cache, pull, and store.
- A file created in one layer and removed in a later one still takes space in the earlier layer. A later chapter covers this in detail.
Compare these two forms:
FROM debian:bookworm-slim
RUN apt-get update
RUN apt-get install -y --no-install-recommends curl
RUN rm -rf /var/lib/apt/lists/*FROM debian:bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/*The first form creates three layers, and the package lists remain stored in the first one. The second creates one layer, and the lists are never stored.
Checking it yourself
Build an image and list its steps:
docker build -t layers-demo .
docker history layers-demoSteps with a size of 0B are metadata only. Steps with a real size added files. With BuildKit, which is the default builder in current Docker releases, some of these entries may show as <missing> for the image ID. This is normal for layers that came from the base image or from a build on another builder.
Common mistakes
- Assuming
ENVorARGshrinks or grows an image. They change metadata only, though a changed value still invalidates the cache for later steps. - Splitting related commands into separate
RUNsteps, which leaves temporary files in earlier layers. - Using
ADDwhenCOPYis enough.ADDhas extra behavior, such as extracting local archives, that can surprise you.
Exercise
In a Dockerfile with FROM, WORKDIR, COPY, RUN, ENV, and CMD, which instructions add files to the image? How would docker history show the others?
Show answer
COPY and RUN add files, because they change the filesystem. WORKDIR, ENV, and CMD only change metadata, so docker history shows them with a size of 0B. FROM contributes the base image's layers.
Exercise
A Dockerfile has three consecutive RUN steps: one downloads a 200 MB archive, one extracts it, and one deletes the archive. Does the final image shrink by 200 MB? Why or why not?
Show answer
No. Each RUN creates its own layer. The download layer still stores the 200 MB archive, and the delete step only hides it in the final filesystem view. Combining the commands into one RUN, or using a multi-stage build, avoids storing it.