Sub topic 6
Choosing between -v and --mount, and mounting read-only
This chapter assumes you can run containers with docker run, know the difference between named volumes and bind mounts, and can read a shell command with line continuations. Docker has two flags for attaching storage. Both do the same job, but they differ in how they report mistakes.
The two syntaxes
-v (or --volume) takes one colon-separated string: source:target:options. --mount takes a comma-separated list of key=value pairs. Here is the same named volume mounted both ways.
docker run -d --name db -e POSTGRES_PASSWORD=example \
-v pgdata:/var/lib/postgresql/data postgres:16
docker run -d --name db2 -e POSTGRES_PASSWORD=example \
--mount type=volume,source=pgdata,target=/var/lib/postgresql/data postgres:16The type key chooses the storage kind: volume, bind, or tmpfs. With -v you don't state the type. Docker infers it from the first field. A bare name becomes a named volume, and a path starting with / becomes a bind mount.
| Aspect | `-v` | `--mount` |
|---|---|---|
| Format | `source:target:options` | `key=value,key=value` |
| Type | Inferred from the source | Explicit `type=` |
| Missing host path for a bind mount | Creates the directory | Returns an error |
| Read-only option | `:ro` | `readonly` (or `ro`) |
| Supports tmpfs | Only through `--tmpfs` | `type=tmpfs` |
The missing-path difference
The most important behavioral difference is what happens when a bind-mount source does not exist. -v silently creates the host directory, usually owned by root. A typo in the path then gives you an empty directory and no warning. --mount fails with an error, which is usually what you want.
# Creates /tmp/typo on the host and starts the container
docker run --rm -v /tmp/typo:/data alpine ls /data
# Fails because the source path does not exist
docker run --rm --mount type=bind,source=/tmp/missing,target=/data alpine ls /dataMounting read-only
A read-only mount lets the container read the data but not change it. Use it for configuration, static sites, and source code that the container should never modify. It limits the damage if the process inside is buggy or compromised.
mkdir -p app && echo '<h1>hello</h1>' > app/index.html
# -v form
docker run --rm -v "$(pwd)/app":/usr/share/nginx/html:ro -p 8080:80 nginx:alpine
# --mount form
docker run --rm \
--mount type=bind,source="$(pwd)/app",target=/usr/share/nginx/html,readonly \
-p 8080:80 nginx:alpineEdits you make to app/index.html on the host show up in the container right away. A write from inside the container fails with Read-only file system. The restriction applies only to that mount. The rest of the container's filesystem stays writable unless you also pass --read-only.
Common errors
- Using spaces after commas in
--mount. The value must be one unbroken string. - Using a relative path as a bind source with
-v. A path likeapp:/datais read as a named volume calledapp. Use./appor$(pwd)/app. - Forgetting quotes around
$(pwd). A path containing spaces splits into several arguments. - Writing
-v pgdata:/data:readonly. The-voption isro, notreadonly.
Which should you use?
Use --mount in scripts, documentation, and anything shared with a team. It is explicit, and it fails loudly. Use -v for quick interactive commands where its brevity helps. Both produce the same mounts.
Exercise
You run docker run --rm -v ./config:/etc/app:ro alpine ls /etc/app and Docker complains that ./config is not a valid volume name, or it mounts an empty volume. What is the likely cause, and how would you write the command with --mount so a wrong path fails instead?
Show answer
Some Docker versions read ./config poorly or treat the source as a name, so the mount does not point at your host directory. Use an absolute path, for example $(pwd)/config. With --mount, write --mount type=bind,source="$(pwd)/config",target=/etc/app,readonly. If the directory does not exist, Docker returns an error instead of creating an empty one.