Bind mounts
- Version
- 29
- Review
- Draft
Summary#
A bind mount makes a file or directory from the Docker daemon host visible inside a container. It is writable by default, so changes made in the container change the host files.
Syntax#
docker run --mount type=bind,src=/host/path,dst=/container/path IMAGE
docker run --mount type=bind,src=/host/path,dst=/container/path,readonly IMAGE
docker run --mount type=bind,src=/host/path,dst=/container/path,bind-create-src IMAGE
docker run --volume /host/path:/container/path:ro IMAGE
Example#
The example uses a local alpine:3 image and a temporary directory visible to the Docker daemon.
(
set -eu
name="dlr-docker-commands-bind-$$"
dir=$(mktemp -d)
trap 'docker rm -f "$name" >/dev/null 2>&1 || true; rm -rf -- "$dir"' EXIT
printf 'from host\n' > "$dir/input.txt"
docker run --pull never --rm --name "$name" --user "$(id -u):$(id -g)" \
--mount "type=bind,src=$dir,dst=/work" \
alpine:3 sh -c 'cat /work/input.txt; echo "from container" > /work/output.txt'
cat "$dir/output.txt"
docker run --pull never --rm --name "$name" --mount "type=bind,src=$dir,dst=/work,readonly" \
alpine:3 sh -c 'if echo changed 2>/dev/null > /work/input.txt
then echo "write allowed"; else echo "write blocked"; fi'
cat "$dir/input.txt"
if ! docker run --pull never --rm --name "$name" \
--mount "type=bind,src=$dir/missing,dst=/work" alpine:3 true 2>/dev/null; then
echo "missing source rejected"
fi
)
Expected output#
from host
from container
write blocked
from host
missing source rejected
Common mistakes#
With --volume, Docker creates a missing host path as a directory, even when you meant a file. --mount reports an error by default. Add bind-create-src to opt into directory creation. Mounting over a non-empty container directory hides its image files.
Version applicability#
Checked with Docker Engine 29 on Docker Desktop for macOS. The path is resolved on the daemon host, so a remote daemon cannot see files on your client. Docker Desktop must be allowed to share the path. The Engine API added bind source directory creation in v1.42.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0