The Docker volume mount command uses the -v or --mount flag in docker run to attach a named volume, host directory, or tmpfs to a container. Use -v for simplicity or --mount for explicit, readable syntax. Both achieve persistent storage or file sharing between host and container.
How to Use -v to Mount a Volume
The -v flag follows the format . The source can be a named volume, a host path, or omitted for anonymous volumes. The container path is the directory inside the container where files appear. Options are comma-separated and include ro for read-only, for read-write (default), and z or Z for SELinux context.
Example: docker run -d -v myvol:/data nginx creates a named volume myvol if it does not exist and mounts it to /data in the container. The container can read and write to /data, and files persist after container stops.
If the source is a host path, use absolute paths. Example: docker run -v /home/user/config:/etc/app/config:ro nginx mounts the local /home/user/config directory to /etc/app/config in read-only mode. Relative paths are not supported and cause errors.
How to Use --mount for Explicit Volume Attachment
The --mount flag uses a key-value syntax: --mount type=bind,source=/host/path,destination=/container/path,readonly. It supports type=volume, bind, or tmpfs. Unlike -v, --mount requires explicit keys and does not allow shorthand.
Example: docker run --mount type=volume,source=myvol,destination=/data nginx attaches the named volume myvol to /data. To make it read-only, add readonly without a value: --mount type=volume,source=myvol,destination=/data,readonly.
--mount is stricter and fails if the source does not exist, while -v may create it silently. Use --mount in production scripts for clarity and safety.
Difference Between -v and --mount Syntax
| Feature | -v | --mount |
|---|---|---|
| Syntax style | Positional, colon-separated | Key-value pairs |
| Volume creation if missing | Creates named volumes automatically | Requires volume to exist unless volume-opt used |
| Error on missing source | May create or fail silently | Fails explicitly |
| Read-only flag | ro |
readonly |
| SELinux support | z, Z |
Not supported |
| Compatibility | All Docker versions | Docker 17.06+ |
Use -v for quick commands and scripts where brevity matters. Use --mount in production environments where clarity and safety are critical.
Bind Mounts vs Named Volumes: When to Use Each
Bind mounts link a host directory directly to a container path. They are useful for development, configuration files, or logs that must be accessible on the host. Example: mounting /var/log/app on the host to /logs in the container ensures logs persist and are readable outside Docker.
Named volumes are managed by Docker and stored in /var/lib/docker/volumes/ on Linux. They are ideal for databases, application data, and production storage. Docker handles permissions and location, reducing host dependency.
Use bind mounts when you need direct host access, such as editing source code in real time. Use named volumes for stateful services like PostgreSQL or Redis, where data isolation and portability matter.
tmpfs mounts store data only in host memory and are never written to disk. Use --tmpfs /tmp or --mount type=tmpfs,destination=/tmp for temporary session data or cache. Data is lost when the container stops.
How to Create and Manage Named Volumes
Create a named volume with docker volume create myvol. This pre-creates the volume and allows setting options like driver or labels. List volumes with docker volume ls. Inspect details with docker volume inspect myvol, which shows mount point, driver, and scope.
To remove a volume, use docker volume rm myvol. Volumes are not deleted when containers stop unless --rm is used with -v and the volume is anonymous. Anonymous volumes are removed only when the container is removed with --rm.
To back up a named volume, start a container that mounts it and use tar. Example: docker run --rm -v myvol:/data -v /backup:/backup ubuntu tar czf /backup/data.tar.gz -C /data .. Restore by extracting the archive into a new volume.
Permissions and Ownership in Volume Mounts
Files in a volume inherit the ownership and permissions of the source. If the container runs as user appuser (UID 1000), it must have write access to the mounted directory. On Linux, ensure the host directory is writable by the UID used in the container.
Example: if the container process runs as UID 1001, run sudo chown -R 1001:1001 /host/path on the host before mounting. Failure to match UID causes permission denied errors.
SELinux can block access even with correct permissions. On SELinux-enabled systems, add :Z for private content or :z for shared content to the -v flag. Example: -v /host/data:/data:Z. This relabels the content for the container’s security context.
Common Errors and How to Fix Them
"Permission denied" on mounted directory: Verify the container process UID matches the directory ownership on the host. Use ls -l to check permissions and chown to correct them.
Mount source does not exist: For bind mounts, ensure the host path exists. Create it with mkdir -p /path/to/dir. For named volumes, create with docker volume create or use -v which creates it automatically.
Container fails to start with invalid volume specification: Check the syntax. In -v, do not include spaces around colons. In --mount, all keys must be valid and comma-separated. Avoid trailing commas.
Data not persisting: Confirm the mount path in the container matches where the application writes data. Applications that write to /app/logs will not use a volume mounted at /data.
SELinux blocking access: Test by temporarily setting setenforce 0. If the mount works, restore enforcement and use :z or :Z in the volume flag.