Named Volumes
What is a named volume?
Section titled “What is a named volume?”A named volume is a storage unit that Docker creates and manages on your behalf. Docker stores volume data under its own directory (typically /var/lib/docker/volumes/ on Linux). You refer to the volume by name — never by a host path.
Because Docker owns the storage location, named volumes:
- Work identically on Linux, macOS, and Windows.
- Survive
docker rm— the volume is separate from the container. - Can be shared between multiple containers simultaneously.
- Are the recommended way to persist application data in production.
Managing volumes
Section titled “Managing volumes”Create a volume
Section titled “Create a volume”docker volume create mydataList all volumes
Section titled “List all volumes”docker volume lsExpected output:
DRIVER VOLUME NAMElocal mydataInspect a volume
Section titled “Inspect a volume”docker volume inspect mydata[ { "CreatedAt": "2024-06-01T10:00:00Z", "Driver": "local", "Mountpoint": "/var/lib/docker/volumes/mydata/_data", "Name": "mydata", "Scope": "local" }]Mountpoint is where Docker stores the actual data on the host. You rarely need to access it directly — Docker abstracts it away.
Remove a volume
Section titled “Remove a volume”docker volume rm mydataA volume cannot be removed while a container is using it. Remove the container first, or use docker volume prune to delete all unused volumes at once.
Mounting a volume into a container
Section titled “Mounting a volume into a container”There are two syntaxes. Both are equivalent — use whichever you find more readable.
Short -v syntax:
docker run -d --name myapp -v mydata:/app/data myimageLong --mount syntax (explicit and recommended for scripts):
docker run -d --name myapp \ --mount type=volume,source=mydata,target=/app/data \ myimageIf the volume mydata does not exist yet, Docker creates it automatically when you run the container.
Data persists across container replacements
Section titled “Data persists across container replacements”The key property of a named volume: you can delete the container and recreate it, and all the data written to the volume is still there.
# Run a container and write datadocker run --name writer -v mydata:/app/data alpine sh -c "echo 'persistent' > /app/data/file.txt"
# Remove the containerdocker rm writer
# Start a brand-new container mounting the same volumedocker run --rm -v mydata:/app/data alpine cat /app/data/file.txt# Output: persistentHands-on practice
Section titled “Hands-on practice”The snippet below runs a container, writes data to a named volume, removes the container, and mounts the same volume in a new container to prove the data survived.
# 1. Create a named volume
docker volume create demo-vol
# 2. Write data into the volume via a container
docker run --rm -v demo-vol:/data alpine sh -c "echo 'data survives!' > /data/message.txt"
# 3. Read it back from a completely new container
docker run --rm -v demo-vol:/data alpine cat /data/message.txt
# 4. Inspect the volume
docker volume inspect demo-vol
# 5. Clean up
docker volume rm demo-vol