1. List and Create a Volume
Before you begin, list existing volumes (you should see none):testvol:
2. Mounting a Volume with -v
You can mount testvol into a container at /yogesh using the shorthand -v flag:
The
-v shorthand syntax is concise but less explicit than --mount. For new scripts, consider using --mount (see Section 5).3. Inspect Volume Details
On the host, inspecttestvol to discover its data directory:
4. Removing Containers vs. Volumes
Stopping or removing the container does not delete the attached volume:Removing a volume deletes all data stored in it. Make sure you no longer need its contents before running
docker volume rm.5. Mounting a Volume with --mount
For clearer syntax and advanced options, use --mount:
Comparing -v vs --mount
6. Handling Volume-in-Use Errors
If you try to remove a volume that’s still attached, you’ll see an error:7. Read-Write vs Read-Only Mounts
By default, mounts are read-write (rw). Here’s how to confirm:
ro):
8. Bind Mounts
Bind mounts let you map any host directory directly into a container:bindtest, the path /yogesh is backed by /data on the host. Note that bind mounts do not show up in docker volume ls.
Use bind mounts when you need to share host files or configuration with a container. For managed, portable storage, prefer Docker volumes.