- Published on
Docker RUN --mount
When you use --mount in a Dockerfile, it must appear immediately after RUN, before the command. This is required because --mount is a flag to RUN, not part of the shell command.
❌ Incorrect
RUN echo "Hello" --mount=type=cache,target=/root/.cache
RUN apt-get update && \
--mount=type=cache,target=/var/lib/apt \
apt-get install -y curl
✅ Correct
RUN \
echo "Hello"
RUN \
apt-get update && apt-get install -y curl
Common --mount types
RUN
RUN
RUN
Important requirements
BuildKit must be enabled
DOCKER_BUILDKIT=1 docker build .or Docker Desktop (enabled by default).
Syntax must be exactly
RUN <command>
Why Docker is strict
Docker parses the Dockerfile before running a shell. Anything after RUN that starts with -- is treated as a RUN option, not shell syntax.