8.2 Docker Volumes
A focused guide to Docker Volumes, connecting core concepts with practical Docker and container operations.
Docker volumes are Docker-managed storage objects with a lifecycle independent of any specific container, providing the standard, recommended mechanism for persisting data that genuinely needs to survive beyond an individual container's removal.
Creating and Using a Volume
A volume can be created explicitly, or created automatically the first time it's referenced by a container.
docker volume create mydata
docker run -d -v mydata:/app/data myapp:1.0
Why Volumes Solve the Writable Layer's Persistence Problem
Unlike a container's own writable layer, which is deleted when the container is removed, a volume's data persists independently, available to be reattached to a new container created later.
docker run -d --name app1 -v mydata:/app/data myapp:1.0
docker rm -f app1
docker run -d --name app2 -v mydata:/app/data myapp:1.0
docker exec app2 ls /app/data
Data written by app1 remains accessible to app2, despite app1 having been completely removed in between.
Listing and Inspecting Volumes
Existing volumes, and details about each one, can be reviewed directly.
docker volume ls
docker volume inspect mydata
Removing a Volume When Its Data Is Genuinely No Longer Needed
Unlike a container's writable layer, a volume must be explicitly removed — it does not disappear automatically just because a container that once used it was removed.
docker volume rm mydata
Why Docker Volumes Matter
Volumes provide the proper mechanism for any data that genuinely needs to outlive a specific container's lifecycle, addressing the writable layer's fundamental ephemerality and providing a managed, independently lifecycled storage option for stateful application needs.