Bind mount

2 snippets in Docker

DKDocker

Volume Mounts (-v)

DK · Run Options
Syntax
docker run -v <host_path>:<container_path>[:<options>] <image>
docker run -v <volume_name>:<container_path> <image>
Example
docker run -v ./src:/app/src myapp:1.0
docker run -v pgdata:/var/lib/postgresql/data postgres:16
docker run -v ./config.json:/app/config.json:ro myapp:1.0

Note Host paths starting with ./ or / create bind mounts. A plain name like pgdata creates or reuses a named volume. Append :ro for read-only. If the host path does not exist, Docker creates it as a directory (not a file), which often causes surprises.

Bind Mounts

DK · Volumes & Storage
Syntax
docker run -v <host_path>:<container_path>[:<options>] <image>
docker run --mount type=bind,source=<path>,target=<path> <image>
Example
docker run -v $(pwd)/src:/app/src myapp:1.0
docker run --mount type=bind,source=$(pwd)/config,target=/app/config,readonly myapp:1.0

Note Bind mounts give the container direct access to host files - changes are reflected immediately in both directions. On macOS and Windows, bind mount performance can be noticeably slow for large node_modules directories.

Frequently asked questions

How does Docker handle bind mount?
Docker covers this with 2 copy-ready snippets on this page. The "Volume Mounts (-v)" snippet in Docker uses `docker run -v <host_path>:<container_path>[:<options>] <image>`.
Which command does the Docker example use?
The "Volume Mounts (-v)" snippet uses `docker run -v <host_path>:<container_path>[:<options>] <image>`, from the Run Options section of the Docker cheat sheet.
What other Docker snippets are shown for "bind mount"?
Besides "Volume Mounts (-v)", this page also shows "Bind Mounts".
Is there anything to watch out for?
Yes. For "Volume Mounts (-v)": Host paths starting with ./ or / create bind mounts. A plain name like pgdata creates or reuses a named volume. Append :ro for read-only. If the host path does not exist, Docker creates it as a directory (not a file), which often causes surprises.