You cannot mount a volume during docker build; the -v flag is a docker run option and Docker's builder ignores host directories, so files must be copied in with COPY or ADD instead. The build context is the only host data the builder can read, and it is sent to the daemon before any instruction runs. To get host files into an image, put them in the context and copy them; to share data at runtime, mount a volume with docker run -v or a Compose volumes: entry.
Why docker build -v Fails and What the Error Means
Docker's command line parses docker build -v /host:/container . as an unknown flag and exits with unknown shorthand flag: 'v' in -v before the build starts. The build command accepts options such as -t, -f, --build-arg, --target and --no-cache, but no bind-mount flag for arbitrary host paths. The reason is architectural: docker build sends the build context (the directory you name, usually .) to the Docker daemon, and every COPY or ADD instruction resolves paths relative to that context, not to your live filesystem. A bind mount would let a Dockerfile read files the daemon never received, which breaks reproducible builds and remote daemon setups. If you see a Dockerfile line like RUN --mount=type=bind,source=/host/path,target=/mnt, that is BuildKit syntax and the source must still be inside the build context; an absolute host path outside the context fails with failed to compute cache key: ... not found.
How to Get Host Files Into an Image: COPY and ADD
Put the files under the directory you pass as the build context, then copy them in a layer. A typical pattern is COPY ./config/app.conf /etc/app/app.conf. Paths on the left are relative to the context root; paths on the right are absolute inside the image. COPY handles plain files and directories and is preferred; ADD additionally unpacks local tar archives and can fetch URLs, which makes layer caching less predictable, so use COPY unless you need those behaviors. Two practical rules: add a .dockerignore file listing node_modules, .git, *.log and build output so the context stays small, and copy dependency manifests before source code (COPY package.json ./ then RUN npm install, then COPY . .) so a source edit does not invalidate the install layer. If a file changes often, copying it late in the Dockerfile keeps earlier layers cached.
BuildKit RUN --mount: The Closest Thing to a Build-Time Mount
With BuildKit enabled, a RUN instruction can mount a directory from the build context, a cache, or a secret for the duration of that one command without leaving it in the final image. The common forms are RUN --mount=type=bind,source=.,target=/src (read-only context files), RUN --mount=type=cache,target=/root/.cache/go-build (persistent cache across builds) and RUN --mount=type=secret,id=npmrc,target=/root/.npmrc (a secret passed with --secret id=npmrc,src=.npmrc). The source for a bind mount is relative to the build context, so source=/etc does not mean the host's /etc. These mounts exist only while the RUN command executes; nothing is persisted in the image layer. BuildKit is the default builder in current Docker releases, but if RUN --mount returns a syntax error, the legacy builder is active and you need to enable BuildKit in Docker's settings or set the environment variable DOCKER_BUILDKIT=1 before building.
Mounting Volumes at Runtime With docker run -v and --mount
Volumes belong to containers, not images. Start the container with docker run -v /host/data:/app/data myimage for a bind mount, or docker run -v myvolume:/app/data myimage for a named volume. The newer syntax is docker run --mount type=bind,source=/host/data,target=/app/data or --mount type=volume,source=myvolume,target=/app/data. Use absolute host paths for bind mounts; a relative path is rejected. Add :ro for read-only access, as in -v /host/config:/app/config:ro. On Linux, files created in a bind mount keep the container user's UID and GID on the host, which can cause permission errors; named volumes avoid most of this because Docker manages ownership. On Docker Desktop for Mac and Windows, bind mounts pass through a file-sharing layer, so large directory trees are slower than named volumes.
Bind Mount vs Named Volume vs COPY: Which to Use
| Method | When it applies | Persists after container removal | Typical use |
|---|---|---|---|
COPY/ADD | Build time | Baked into image layer | Config files, app code, static assets |
Bind mount (-v /host:/container) | Run time | Yes, host directory survives | Editing code live, reading host config |
Named volume (-v name:/container) | Run time | Yes, until docker volume rm | Databases, uploads, caches |
RUN --mount=type=bind | Build time, one RUN step | No | Reading context files without copying |
RUN --mount=type=cache | Build time, one RUN step | Yes, as builder cache | Package manager and compiler caches |
The decision rule: if the file must exist inside the image for it to run anywhere, use COPY. If the data changes after the image is built or must outlive the container, use a volume at run time. If a build step needs to read context files without adding them to a layer, use a BuildKit bind mount.
Docker Compose Volumes During Build and Run
Compose splits the two phases clearly. Under a service, build: controls the image build and accepts context, dockerfile, args and target; a volumes: entry under the same service applies when the container runs, not while the image is built. A common mistake is expecting volumes: to make files available to a RUN step; it does not. Compose also supports BuildKit mount syntax through the build: section, for example a secret passed with secrets: at the service level and consumed by RUN --mount=type=secret. For development, mount source code over the copied version at run time (volumes: - ./src:/app/src) so edits appear without rebuilding; keep the COPY in the Dockerfile for production images where no bind mount exists.
Common Errors and How to Fix Them
COPY failed: file not found in build context means the path is outside the context or excluded by .dockerignore; move the file under the context directory or adjust the ignore file. invalid mount config for type "bind": bind source path does not exist comes from a run-time bind mount whose host path is missing; create the directory first. Permission denied inside a container writing to a bind mount usually means the host directory is owned by a different UID; either change ownership on the host, run the container with a matching user, or switch to a named volume. If RUN --mount is rejected, confirm BuildKit is active. If a file copied with COPY seems stale, remember that Docker caches layers by content; changing the file changes the checksum and rebuilds that layer, but changing only file metadata may not.