Skip to content

Named Volumes

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.
Terminal window
docker volume create mydata
Terminal window
docker volume ls

Expected output:

DRIVER VOLUME NAME
local mydata
Terminal window
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.

Terminal window
docker volume rm mydata

A 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.

There are two syntaxes. Both are equivalent — use whichever you find more readable.

Short -v syntax:

Terminal window
docker run -d --name myapp -v mydata:/app/data myimage

Long --mount syntax (explicit and recommended for scripts):

Terminal window
docker run -d --name myapp \
--mount type=volume,source=mydata,target=/app/data \
myimage

If 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.

Terminal window
# Run a container and write data
docker run --name writer -v mydata:/app/data alpine sh -c "echo 'persistent' > /app/data/file.txt"
# Remove the container
docker rm writer
# Start a brand-new container mounting the same volume
docker run --rm -v mydata:/app/data alpine cat /app/data/file.txt
# Output: persistent

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
What happens to a named volume when you run `docker rm` on the container using it?
Which flag mounts a named volume using the short syntax?
What does `docker volume prune` do?
If you specify a volume name that does not exist in a `docker run` command, what does Docker do?