docker run -v mounts storage into a container using the form -v SOURCE:TARGET[:OPTIONS]: the left side is a host path or a volume name, the middle is the path inside the container, and the optional third field holds settings such as ro. Docker decides between a bind mount and a named volume by looking at what the left side is.
docker run -v mydata:/data alpine # named volume
docker run -v "$(pwd)"/site:/usr/share/nginx/html:ro nginx # bind mount, read-only
docker run -v /data alpine # anonymous volume
The rest of this page takes the flag apart field by field. If you want the wider question of when to pick a volume or a bind mount, see Docker volume vs bind mount; this article is about writing the flag correctly.
The three fields of docker run -v
The fields are separated by colons and must stay in this order. Docker's bind mount page describes the format as -v <host-path>:<container-path>[:opts], and the volumes page gives the volume version as -v [<volume-name>:]<mount-path>[:opts].
| Field | What goes there | Example |
|---|---|---|
| 1. Source | A host path (bind mount) or a name (named volume). Omit it for an anonymous volume. | ./site, mydata |
| 2. Target | Where it appears inside the container | /usr/share/nginx/html |
| 3. Options | Comma-separated list, optional | ro, z, ro,rslave |
Swapping the first two fields is the most common mistake. The host side always comes first, the container side second. In a command like -v /app:/home/alex/project the container sees the host's /app folder at /home/alex/project, not the other way round.
Named volume, bind mount or anonymous volume
Docker does not need a separate flag to tell these apart. It reads the first field:
- A name (
mydata:/data) is a named volume. The volumes documentation says that if the volume doesn't exist, "Docker creates the volume for you", and it starts out empty. - A host path (
/home/alex/site:/siteor./site:/site) is a bind mount of that folder. - No first field (
-v /data) is an anonymous volume. It gets a random name that is unique on the host, and the documentation says anonymous volumes "aren't reused or shared between containers automatically".
One practical consequence: a typo in a volume name quietly creates a second, empty volume. If a database container starts with no data after you recreate it, run docker volume ls and check whether you now have both pgdata and pgdta.
Host paths: absolute, relative and Windows
A bind mount source must identify a location on the machine where the Docker daemon runs. Three forms come up constantly.
Absolute paths
These work everywhere on Linux and macOS, and the Docker reference uses the same shell trick to build one from the current directory:
docker run -v "$(pwd)":"$(pwd)" -w "$(pwd)" -i -t ubuntu pwd
That command (adapted from the docker run reference, which writes it without quotes) mounts the current directory at the same path inside the container and sets it as the working directory. The quotes protect paths that contain spaces.
Relative paths
The docker run reference states that since Docker Engine 23, relative host paths are supported, and shows this example:
docker run -v ./content:/content -w /content -i -t ubuntu pwd
Keep the leading ./: a bare content:/content starts with a name, so Docker treats it as a named volume called content, not as a folder. On an older engine, use an absolute path such as "$(pwd)"/content in Bash or Zsh.
Windows paths
For Windows containers, the docker run reference says paths must use Windows semantics, for example c:\foo:c:\dest. It lists several cases that fail: a mapped network drive or a UNC path as the source, a file as the source (it must be a directory), and c: as the destination. The destination must also be a non-existing or empty directory, or be on a drive other than C:. With Linux containers on Docker Desktop, the target is still a Linux path; only the source is a Windows path such as C:\Users\alex\site.
What happens when the host path doesn't exist
With -v, Docker creates it. The bind mount documentation says the host directory is created automatically and is always created as a directory, even if you meant it to be a file. The docker run reference shows the same thing:
docker run -v /doesnt/exist:/foo -w /foo -i -t ubuntu bash
If the daemon has no permission to create the folder, you create it yourself first. The side effect is that a typo in the path gives you an empty folder instead of an error, and the container then sees an empty directory where you expected your files. The --mount flag does the opposite and fails with invalid mount config for type "bind": bind source path does not exist, unless you add bind-create-src.
The options field: ro, z, Z and propagation
The third field takes a comma-separated list. These are the options the Docker documentation lists for bind mounts, plus the ones for volumes:
| Option | Applies to | Effect |
|---|---|---|
ro (or readonly) | Bind mounts and volumes | The container cannot write to the mount |
z | Bind mounts | SELinux label, content shared among containers |
Z | Bind mounts | SELinux label, content private to this container |
rprivate (default), private, rshared, shared, rslave, slave | Bind mounts | Bind propagation, Linux hosts only |
volume-nocopy | Volumes | Don't copy the image's existing files at the target into an empty volume |
Combine options with commas, not extra colons: -v "$(pwd)"/target:/app2:ro,rslave. Docker's own example for the bind mounts page runs two mounts of the same folder, one read-write and one read-only with rslave propagation.
Read-only mounts
docker run -d -it --name devtest -v "$(pwd)"/target:/app:ro nginx:latest
Use ro whenever the container only needs to read, such as config files or static assets. A container with read-write access to a bind mount can create, change and delete files on the host.
SELinux labels
On hosts that enforce SELinux, a bind mount may be unreadable inside the container until it is relabelled. -v "$(pwd)"/target:/app:z relabels it as shared. The Docker documentation warns that the label changes the host folder itself, with effects beyond Docker, and that Z on system directories such as /home or /usr can make the host unusable. Label only folders that exist for the container's sake. Note that --mount cannot set SELinux labels, which is one reason to keep -v for this case.
Propagation
The default, rprivate, means mounts made later inside the bind-mounted folder are not shared with the other side. Propagation can only be configured for bind mounts on Linux hosts, and the documentation says it doesn't work with Docker Desktop. Leave it alone unless you know you need it.
Mounting over a folder the image already uses
A mount covers whatever the image has at the target path. Docker's bind mount page demonstrates the extreme case: mounting a folder over /usr leaves a container that cannot start, with an error ending in executable file not found in $PATH.
docker run -d -it --name broken-container -v /tmp:/usr nginx:latest
Named volumes behave differently. When the volume is empty, Docker copies the image's existing files from the target path into it, which is why -v pgdata:/var/lib/postgresql/data works on first start. Add volume-nocopy in the third field to turn the copy off.
Cleaning up: what --rm removes
Volumes outlive containers. The volumes documentation says an unused volume isn't removed automatically; use docker volume rm NAME for one volume or docker volume prune for all unused ones. The exception is anonymous volumes with --rm. The docker run reference gives this example:
docker run --rm -v /foo -v awesome:/bar busybox top
When this container is removed, the anonymous /foo volume is deleted too, while the named awesome volume stays. This is also why a throwaway container started without --rm and with bare -v /data leaves a nameless volume behind each time. docker volume ls will show them as long random names.
Common errors and fixes
- Empty folder in the container. The source path was wrong and Docker created a new empty directory, or the mount hides files the image put there. Check the first field against
pwdand check what the image has at the target. - Permission denied inside the container. On an SELinux host, try the
zoption on a folder you own. Otherwise compare the file owner on the host with the user the container runs as. - Edits on the host don't appear. You may have used a name instead of a path, so Docker made a volume. Write
./nameor an absolute path. - Container exits right after start. A mount over a system folder such as
/usrcan remove the executable the container needs.
For the other half of most docker run lines, publishing ports, see Docker port mapping, and for the wider list of commands, the Docker commands cheat sheet.
When to switch to --mount
The --mount form names every field, so nothing depends on colon order. The same bind mount reads --mount type=bind,source="$(pwd)"/target,target=/app,readonly. Keep -v when you want the short form, and remember that anything -v accepts in the third field has a counterpart there, apart from SELinux labels, which only -v can set.
Frequently asked questions
- What is the difference between docker run -v and --mount?
- Both attach storage to a container. -v packs everything into one colon-separated string, while --mount uses named key=value fields such as type, source and target. One documented difference: with -v, a missing bind-mount source directory is created for you, while --mount returns an error unless you add bind-create-src.
- Does docker run -v create the host directory if it doesn't exist?
- Yes. Docker's bind mount documentation says -v creates a missing host path, and it is always created as a directory. If the Docker daemon lacks permission to create it, you have to create the directory yourself before starting the container.
- Can I use a relative path with docker run -v?
- Yes, since Docker Engine 23, according to the docker run reference. The reference shows -v ./content:/content as an example. On older engines, use an absolute path such as -v "$(pwd)"/content:/content.
- How do I make a mount read-only?
- Add ro as the third field: -v "$(pwd)"/target:/app:ro for a bind mount, or -v mydata:/data:ro for a named volume. The container can then read the files but any write fails.
- What does the :z or :Z suffix do?
- They relabel the mounted files for SELinux. z marks the content as shared between containers, Z marks it as private to one container. Docker warns that the label changes the host files themselves, so Z on system directories such as /home or /usr can make the host unusable.