Docker bind mounts: share a host directory
Lab prerequisites
Use only an approved disposable, offline Linux VM with a local Docker Engine, ordinary local volume driver, and reviewed preloaded busybox:1.37 (record its digest, platform, exact Engine version and required utilities first). Verify the active Docker context and endpoint; every bm-v06 resource name in the lesson must be unused before starting. Never reuse a pre-existing volume. Use a dedicated lab shell, run commands individually in order, and stop on unexpected errors (the explicitly labeled negative tests are exceptions). Only delete resources you created after confirming their identity. All outputs are expected, not observed; these are manual labs, not ByteMind runnable editors. Before contacting the Engine, verify the effective local endpoint including DOCKER_HOST, DOCKER_CONTEXT and CLI overrides. Recheck the approved local image ID and native platform; if the recorded digest is unavailable, report it as unknown rather than inventing one. Use owner-set resource and time limits. If prerequisites cannot be confirmed, do the paper exercise instead. Check that the approved fixture has no declared image volumes and that the synthetic destinations and file names are unused. The bind-mount lesson alone uses a fresh host directory; do not extend it to other paths.
One directory, two views
A bind mount makes an existing daemon-host file or directory accessible through a different path in a container. It is useful for sharing editable source code or configuration. Unlike a named volume, you choose the host path directly. The mount does not copy the directory into the image. A writable mount allows the container to modify those host files.
Read a small fixture
In the local VM terminal, create a fresh directory with mktemp. The quoted argument passes the source path as a single shell argument. Mount the fixture read-only for this first example. Expect green. Change the text on the host and rerun the read command; expect orange. Both containers see the host file rather than separate copies. Use a private fresh directory with no nested mounts, symlinks or concurrent writers. Verify that the path contains no comma because --mount parses comma-separated fields, even when quoted. If not, stop before invoking Docker.
lab=$(mktemp -d) || exit 1printf 'green\n' > "$lab/note"docker run --rm --pull=never --network none --mount "type=bind,src=$lab,dst=/data,readonly" busybox:1.37 cat /data/noteprintf 'orange\n' > "$lab/note"docker run --rm --pull=never --network none --mount "type=bind,src=$lab,dst=/data,readonly" busybox:1.37 cat /data/noteMissing paths and safe boundaries
With the default --mount behavior a nonexistent bind source is an error; the short -v form can create a missing directory, which can hide a typo. On a remote daemon the source is not the client computer. Desktop has additional file-sharing rules. A mount may hide existing destination files. Never share system directories, credentials or the Docker socket. After both containers exit, remove only the new fixture file and its empty directory in the same shell. Keep the same private shell and confirm the exact directory created by mktemp before cleanup. Read-only bind mounts with nested mounts have kernel-dependent recursive behavior; Linux kernels before 5.12 may leave submounts writable. This fixture has no submounts.
# First confirm lab is the exact private directory you created.if [ -n "$lab" ] && [ -d "$lab" ] && [ ! -L "$lab" ] && [ -f "$lab/note" ] && [ ! -L "$lab/note" ]; then rm -- "$lab/note" && rmdir -- "$lab"fiManual practice
Predict what happens if the host text changes after container creation, and explain why a read-only mount does not prevent the host from editing its own file.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Docker bind mounts: share a host directory — Knowledge check
Apply the concepts from this lesson.
What does a bind mount use as its source?
What is the normal behavior of --mount when the bind source does not exist?
A host edits the same file in place inside a directory mounted read-only into a container. Can the container read the new content?