A Docker Volume is persistent storage that keeps data outside the container. When a container is deleted, the volume and its data survive.


The Problem Without Volumes

Every time you run a container, it starts fresh. Any data created inside is lost when the container stops or is removed.

docker run -it myapp
# create some data inside container
# exit and remove container

docker rm <container_id>
# data is gone forever

Solution — Named Volume

# Create volume
docker volume create user-data

# Run container with volume
docker run -it -v user-data:/app myapp
# create data inside /app
# exit and remove container

docker rm <container_id>
# data still exists in the volume

Real Example — Data Persisting Across Runs

Without volume — each run is a fresh start:

docker run -it user-storage:v1
# Enter: Sham → shows: Sham

docker run -it user-storage:v1
# Enter: Paul → shows: Paul only (Sham is gone)

With volume — data builds up across runs:

docker run -it -v user-data:/app user-storage:v1
# Enter: Sham → shows: Sham

docker run -it -v user-data:/app user-storage:v1
# Enter: Paul → shows: Sham, Paul

docker run -it -v user-data:/app user-storage:v1
# Enter: Daksh → shows: Sham, Paul, Daksh

Volume Commands

# Create a volume
docker volume create user-data

# List all volumes
docker volume ls

# See details and location of a volume
docker volume inspect user-data

# Remove a volume
docker volume rm user-data

# Remove all unused volumes
docker volume prune