Working with Docker Volumes
1. Creating Named Volumes
| Command | Description | Example |
|---|---|---|
| docker volume create | Create a new named volume | docker volume create my-volume |
| docker run -v | Mount a volume when running a container | docker run -v my-volume:/data nginx |
| docker run --mount | Mount a volume using the mount flag (modern approach) | docker run --mount source=my-volume,target=/data nginx |
Example: Create and Use a Named Volume
docker volume create my-app-data
docker run -d --name my-app -v my-app-data:/app/data nginx
Note: Named volumes are managed by Docker and persist even if the container is removed.
2. Listing Volumes
| Command | Description | Options |
|---|---|---|
| docker volume ls | List all volumes | -q - Quiet mode, only show volume names--filter - Filter output--format - Format output |
| docker volume inspect <volume_name> | Inspect a specific volume | --format - Format output--verbose - Verbose output |
| docker volume prune | Remove all unused volumes | -f - Force removal without confirmation--filter - Filter volumes to prune |
3. Inspecting Volumes
| Command | Description | Example |
|---|---|---|
| docker volume ls | List all volumes | docker volume ls |
| docker volume inspect <volume_name> | Display detailed information about a specific volume | docker volume inspect my-volume |
| docker volume prune | Remove all unused volumes | docker volume prune |
Note: The inspect command returns a JSON object with volume details like driver, mount point, and labels.
4. Mounting Volumes
| Option | Description | Example |
|---|---|---|
| -v | Mount a volume (short form) | docker run -v /host/path:/container/path image |
| --mount | Mount a volume (long form) | docker run --mount type=bind,source=/host/path,target=/container/path image |
| -v /host/path:/container/path:ro | Read-only mount | docker run -v /host/data:/app/data:ro image |
| -v /host/path:/container/path:rw | Read-write mount (default) | docker run -v /host/data:/app/data:rw image |
Example: Mounting a Volume with Read-Only Access
docker run -v /home/user/config:/etc/config:ro myapp
Note: When using
-v, the host path is created if it doesn't exist. With --mount, you must ensure the host path exists.
Warning: Mounting volumes with
:ro mode prevents writes to the volume from within the container, but does not prevent modification of the host directory outside the container.
5. Using Bind Mounts
| Option | Values | Description |
|---|---|---|
| -v source:destination | -v /host/path:/container/path |
Mounts a directory or file from the host to the container |
| --mount type=bind | --mount type=bind,source=/host/path,target=/container/path |
Alternative syntax for bind mounts using the mount flag |
Note: Changes made to the mounted directory on the host are immediately reflected in the container and vice versa.
Warning: Be cautious when using bind mounts with sensitive data, as they can expose host filesystems to containers.
6. Creating tmpfs Mounts
tmpfs mounts allow you to create temporary filesystems in memory, which are useful for storing sensitive data or caching.
| Option | Values | Description |
|---|---|---|
| --tmpfs | /run |
Mounts a tmpfs at the specified path inside the container |
| size | 100m |
Maximum size of the tmpfs mount (e.g., 100m for 100 MB) |
| mode | 1777 |
Permissions for the tmpfs mount (default is 1777) |
Note: tmpfs mounts are not persisted to disk and are cleared when the container stops.
Warning: tmpfs mounts use RAM, so ensure sufficient memory is available.
7. Setting Volume Drivers
| Driver | Description | Usage Example | Notes |
|---|---|---|---|
| local | Default driver; creates volumes on the host filesystem | -v /host/path:/container/path |
Simple and fast, but not portable across hosts |
| bind | Mounts a directory or file from the host into the container | --mount type=bind,source=/host/dir,target=/container/dir |
Can be used for sharing data or configuration files |
| tmpfs | Creates a temporary filesystem in memory (only available on Linux) | --mount type=tmpfs,destination=/tmp |
Fast but volatile; data is lost when container stops |
| volume | Creates named volumes managed by Docker | -v myvolume:/container/path |
More portable and safer than bind mounts |
| plugin | Uses a third-party volume plugin (e.g., for cloud storage) | -v myplugin://volume-name:/container/path |
Requires plugin installation; supports external storage systems |
Example: Using a Custom Volume Driver
docker volume create --driver myplugin --opt size=10G --opt type=ssd myvolume
Note: Volume drivers must be installed and configured on the Docker host before they can be used.
8. Managing Volume Permissions
| Permission Type | Description | Command Example | Notes |
|---|---|---|---|
| Read-Only | Allows reading files but not modifying them | docker run -v /host/path:/container/path:ro image |
Useful for sharing configuration files or read-only data |
| Read-Write | Allows reading and writing to the volume | docker run -v /host/path:/container/path:rw image |
This is the default mode if no mode is specified |
| User Mapping | Maps container user to host user for file permissions | docker run -v /host/path:/container/path:Z image |
The Z flag tells Docker to set SELinux labels for the volume |
Note: When using
ro or rw, the volume must exist on the host before mounting.
Warning: Incorrect volume permissions can lead to data corruption or security vulnerabilities.
9. Backing Up Volumes
| Command | Description | Example |
|---|---|---|
| docker run | Run a container to copy volume data to a tar archive | docker run --rm -v /host/path:/backup -v myvolume:/data alpine tar czf /backup/backup.tar.gz -C /data . |
| docker cp | Copy files or directories from a container to the host | docker cp container:/volume/path /host/backup |
| docker exec | Execute a command inside a running container to backup data | docker exec container tar czf /backup/backup.tar.gz -C /data . |
Example: Backup Volume to Host
# Create a backup of the volume named "myapp_data"
docker run --rm -v /host/backup:/backup -v myapp_data:/data alpine tar czf /backup/data_backup.tar.gz -C /data .
Note: Ensure the backup directory exists and has appropriate permissions before running the command.
Warning: If the volume is in use by a running container, it's safer to stop the container or run the backup from within a temporary container.
10. Restoring Volumes from Backup
Example: Restore volume from backup file
docker run --rm \
-v /backup/data.tar.gz:/backup/data.tar.gz \
-v myapp_data:/volume \
alpine sh -c "cd /volume && tar -xzf /backup/data.tar.gz"
| Step | Description | Action |
|---|---|---|
| 1 | Prepare backup file | Ensure the backup is accessible to the container (e.g., mounted as volume or copied into image) |
| 2 | Start restore container | Run a temporary container with volume mounts for backup and target volume |
| 3 | Extract backup contents | Use tar or other tools to extract backup into the target volume |
Note: Ensure proper permissions and ownership after restore to prevent access issues.
Warning: Always test restore process on a non-production environment before applying to live systems.
Key Considerations
- Backup must be compatible with the application's expected file structure
- Permissions and ownership of restored files should match original setup
- Verify integrity of backup before initiating restore
11. Removing Volumes
| Command | Description | Notes |
|---|---|---|
| docker volume rm <volume_name> | Remove a single named volume | Must be unused by any container |
| docker volume rm <volume1> <volume2> | Remove multiple named volumes | All must be unused |
| docker volume prune | Remove all unused volumes | Does not remove volumes in use by containers |
| docker volume prune -a | Remove all volumes (used and unused) | DEPRECATED in Docker 24.0+, use docker volume prune --filter "all=true" |
Note: Volumes that are currently in use by a running container cannot be removed. Stop the container first using
docker stop <container_name>.
12. Pruning Unused Volumes
| Command | Description | Flags |
|---|---|---|
| docker volume prune | Remove all unused volumes not referenced by any container | -a or --all to remove all volumes, not just dangling ones |
Note: This command removes volumes that are not currently in use by any container. Use with caution as it's irreversible.