Skip to main content
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.

1. List and Create a Volume

Before you begin, list existing volumes (you should see none):
Create a new volume called 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).
Verify the mount inside the container:

3. Inspect Volume Details

On the host, inspect testvol to discover its data directory:
Create sample files inside the volume:

4. Removing Containers vs. Volumes

Stopping or removing the container does not delete the attached volume:
To remove the volume and reclaim space:
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:
Check that your files persist:

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:
Stop and remove the container, then retry:
You can also remove all unused volumes in one command:

7. Read-Write vs Read-Only Mounts

By default, mounts are read-write (rw). Here’s how to confirm:
To mount that same volume as read-only (ro):
Clean up:

8. Bind Mounts

Bind mounts let you map any host directory directly into a container:
Inside 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.

Watch Video