In this tutorial, you’ll learn how to create, inspect, mount, and remove Docker volumes. Volumes are the preferred mechanism for persisting container data, ensuring it lives beyond the lifecycle of individual containers.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
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
| Option | Syntax | When to Use |
|---|---|---|
-v | -v volume_name:/container_path | Quick tests |
--mount | --mount type=volume,source=volume_name,destination=/container_path | Production & scripts |
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.