Sub topic 3
The RUN --mount instruction
This chapter assumes you can write a basic Dockerfile, run docker build, and know that each instruction produces an image layer. It covers the general RUN --mount syntax. Later chapters cover cache mounts and secret mounts in detail.
What a mount does
RUN --mount attaches extra storage to the container that runs a single RUN step. The mount exists only while that command runs. When the command finishes, BuildKit detaches it, and its contents are not part of the layer the step produces.
That is the difference from COPY. A file copied in becomes part of a layer and stays in the image. A file that is mounted is visible to the command and then gone from the image.
--mount is a BuildKit feature. Current Docker versions use BuildKit by default. Put the syntax directive on the first line so the Dockerfile frontend knows the flag:
# syntax=docker/dockerfile:1
FROM alpine:3.20
RUN --mount=type=bind,source=.,target=/src \
ls /srcSyntax
The flag takes comma-separated key=value options:
RUN --mount=type=<type>,<option>=<value>,<option>=<value> <command>You can repeat --mount to attach several mounts to one step. Each mount needs its own target path inside the container.
Mount types
| Type | Purpose | Persists in image? |
|---|---|---|
| `bind` | Read files from the build context or another stage without a `COPY`. This is the default type. | No |
| `cache` | A directory kept between builds, such as a package manager cache. | No |
| `secret` | A credential exposed as a file or environment variable during the step. | No |
| `ssh` | Forwards an SSH agent socket to the step. | No |
| `tmpfs` | An in-memory directory that is empty at the start of the step. | No |
This tutorial focuses on cache, secret, and ssh. The bind type is useful for the idea of "mounted, not copied", so we use it below.
Common options
Several options apply to more than one type:
target(alsodstordestination) is the path inside the container where the mount appears.sourceis the path inside the source being mounted. Forbind, it is a path in the build context or in the stage named byfrom.fromnames a build stage, image, or context to mount from. It applies tobind, and tocacheas its initial content.readonly(alsoro) makes the mount read-only. Abindmount is read-only by default.ididentifies acacheorsecretmount. Secrets are looked up by this id.
Options specific to each type, such as sharing for caches or env for secrets, are covered in the chapters that use them.
Worked example: bind mount from another stage
A bind mount lets a step use files from an earlier stage without copying them into the final image:
# syntax=docker/dockerfile:1
FROM alpine:3.20 AS assets
RUN mkdir /data && echo "build-time data" > /data/info.txt
FROM alpine:3.20
RUN --mount=type=bind,from=assets,source=/data,target=/mnt \
cat /mnt/info.txt > /summary.txt
CMD ["cat", "/summary.txt"]The second stage reads /mnt/info.txt while the step runs, and writes the result to /summary.txt. Only /summary.txt ends up in the layer. The /mnt directory is not in the image.
Common errors
- Unknown flag: mount. The build is not using BuildKit, or the syntax directive is missing or not the first line. Check that
# syntax=docker/dockerfile:1is on line 1, with no blank line or comment before it. - Writing to a read-only mount. Bind mounts are read-only unless you use
rw. Writes to the mount do not carry into the image either way, so write results to a normal path such as/summary.txt. - Expecting the mount in later steps. Each
RUNattaches its own mounts. Repeat the--mountoption on every step that needs it. - Missing target. Every mount needs a
target. Without it, the build fails with a parse error.
Exercise
A Dockerfile step uses RUN --mount=type=bind,source=.,target=/src ls /src. Is the /src directory present in the final image, and why?
Show answer
No. The bind mount is attached only while that RUN command runs. BuildKit detaches it afterward, and only file changes the command made outside the mount are saved in the layer.
Exercise
A build fails with "Unknown flag: mount". Name two things to check.
Show answer
Check that the build uses BuildKit, and that # syntax=docker/dockerfile:1 is the very first line of the Dockerfile.