A container is easy to replace. That is one of Dockerâs biggest strengths. But the same feature creates a serious problem the moment your application stores real data. If you remove a container that has been writing into its own filesystem, that data disappears with it. Named volumes solve this by moving important data outside the container lifecycle.
Named volumes are the standard Docker answer for persistent application storage. They are simple to use, portable across containers, and managed directly by Docker.
A named volume is a Docker-managed storage location with a name you choose, such as postgres-data or uploads. Instead of mounting a host path manually, you tell Docker to attach that volume name to a container path.
docker run -d --name db -v postgres-data:/var/lib/postgresql/data postgres:16In this example:
postgres-data is the named volume/var/lib/postgresql/data is the path inside the containerNamed volumes are useful because they separate data lifecycle from container lifecycle.
A container may be stopped, replaced, recreated, or upgraded. The volume remains until you explicitly remove it.
That makes named volumes ideal for:
Named volumes are usually the right choice when:
A beginner shortcut is this:
docker volume createYou can let Docker create a volume automatically on first use, but it is often helpful to create it explicitly.
docker volume create myvolumeDocker responds with the volume name:
myvolumeNow it exists independently of any container.
Explicit creation helps because you can:
For example:
docker volume create postgres-data
docker volume create app-uploads
docker volume create shared-logsDescriptive names make cleanup and troubleshooting much easier later.
-vThe shortest syntax is -v.
docker run -d \
--name app \
-v myvolume:/app/data \
my-imageThe format is:
-v volume-name:/path/in/containerDocker checks whether myvolume exists:
docker run -d \
--name notes-app \
-p 3000:3000 \
-v notes-data:/app/data \
my-notes-imageIf the app writes files into /app/data, those files stay in the volume even if you remove the container.
--mountThe --mount syntax is longer but clearer and more explicit.
docker run -d \
--name app \
--mount type=volume,source=myvolume,target=/app/data \
my-imageThis says the same thing as the -v example, but the fields are named:
type=volumesource=myvolumetarget=/app/data--mount--mount is easier to read in scripts and documentation, especially once you add more options. With -v, beginners sometimes mix up volume names and host paths. --mount makes the intent explicit.
docker run -v myvolume:/app/dataThis command is worth pausing on because it is one of the most important volume patterns in Docker.
docker run -v myvolume:/app/data my-imageWhat happens?
myvolume/app/data inside the container/app/data is stored in the volumeThe key insight is that the container path acts like an attachment point. The real data is not tied to the container filesystem anymore.
Named volumes become most valuable when you replace containers.
# First container writes data
docker run -d --name db -v pgdata:/var/lib/postgresql/data postgres:16
# Later, remove the container
docker rm -f db
# Recreate a fresh container with the same volume
docker run -d --name db -v pgdata:/var/lib/postgresql/data postgres:16The database files remain because they live in pgdata, not in the removed container.
This pattern is extremely common in upgrades:
One named volume can be mounted into more than one container.
docker volume create shared-data
docker run -d \
--name writer \
-v shared-data:/data \
alpine sh -c "while true; do date >> /data/log.txt; sleep 5; done"
docker run -d \
--name reader \
-v shared-data:/logs:ro \
alpine sh -c "tail -f /logs/log.txt"Here:
writer container writes to the volumereader container mounts the same volume read-only and watches the fileThe safest design is usually:
If multiple containers write to the same files without coordination, data corruption can happen. The volume itself does not provide application-level locking. Your application still needs to handle concurrency correctly.
docker volume lsTo list all volumes on the machine:
docker volume lsExample output:
DRIVER VOLUME NAME
local myvolume
local pgdata
local app-uploadsThis is useful for:
If your machine has many projects, docker volume ls becomes a routine cleanup and troubleshooting command.
docker volume inspectTo see metadata about one volume:
docker volume inspect myvolumeYou usually get JSON output like:
[
{
"CreatedAt": "2026-07-12T12:00:00Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/myvolume/_data",
"Name": "myvolume",
"Options": {},
"Scope": "local"
}
]local for normal single-host useThis is the command you run when you want to answer, âWhere is this volume actually stored?â
On Linux with the local driver, Docker often stores named volume data under a path like:
/var/lib/docker/volumes/<volume-name>/_dataFor example:
/var/lib/docker/volumes/myvolume/_dataOn Docker Desktop for macOS and Windows, Docker usually runs inside a lightweight VM. That means the volume data is not as directly accessible in the same way as on a native Linux host. You should think of Docker as managing the location for you rather than relying on browsing the files manually.
That is one reason named volumes are nice: your workflow does not depend heavily on knowing the exact host path.
The default volume driver is local, and that is what most beginners use.
docker volume create myvolumeThis uses the local driver unless you specify something else.
The local driver stores volume data on the same Docker host. It is perfect for:
Docker also supports other drivers, depending on your environment and plugins. Examples include:
A conceptual example might look like:
docker volume create \
--driver local \
--opt type=nfs \
--opt o=addr=192.168.1.10,rw \
--opt device=:/exports/appdata \
nfs-dataIn production environments, remote or network-backed drivers are useful when data must outlive a single machine or be shared across hosts.
For most learners, though, focus on the local driver first. It teaches the core idea clearly.
docker volume rmTo delete a specific volume:
docker volume rm myvolumeDocker removes the volume only if it is not in use by a container.
If a container still references the volume, Docker refuses:
Error response from daemon: remove myvolume: volume is in useThat safety check helps prevent accidental data loss.
Before removing a volume, make sure:
docker volume pruneOver time, volumes accumulate. Some are still important. Others are leftovers from experiments, deleted containers, or old Compose projects.
To remove all unused volumes:
docker volume pruneDocker asks for confirmation. To skip the prompt:
docker volume prune -fAn âunusedâ volume may still contain important data. Docker only knows whether a volume is attached to a container, not whether you care about the contents. Always inspect before pruning in environments that matter.
Named volumes and bind mounts both store data outside the container, but they serve different needs.
| Topic | Named Volume | Bind Mount |
|---|---|---|
| Managed by | Docker | You |
| Host path knowledge | Usually not needed | Required |
| Portability | Higher | Lower |
| Best for | Persistent app data | Local development and host file injection |
| Safety from accidental host edits | Better | Lower |
If you are storing database files, named volumes are usually the right first choice.
docker run -d --name postgres -v pgdata:/var/lib/postgresql/data postgres:16docker run -d --name app -v uploads:/app/uploads my-appdocker run -d --name app -v shared-logs:/var/log/app my-app
docker run -d --name collector -v shared-logs:/logs:ro alpine tail -f /logs/app.logThese examples all share one theme: the data should persist even if the container changes.
Prefer postgres-data over data1. Clear names reduce mistakes.
A named volume is persistent, but it is not magical. You can still delete it. Persistence is not the same as backup.
Let Docker and your containers manage the data unless you have a specific reason to intervene.
A volume does not protect you from concurrent write issues.
Unused volumes quietly consume disk space. Periodic inspection and pruning keep your host healthy.
Assuming deleting a container deletes the volume It does not. Named volumes persist independently.
Using bind mounts for database data without a reason This often adds host-path complexity you do not need.
Removing a volume before checking its contents That can destroy important application state.
Forgetting that Docker can auto-create volumes This can lead to accidental anonymous or unexpected volumes if you mistype a name.
Expecting a volume to solve application-level file locking It only provides shared storage, not concurrency control.
Named volumes are the default Docker answer for persistent container data. They are simple, reliable, and designed for the exact problem containers create: disposable compute with durable storage. Learn the docker volume create, docker volume ls, docker volume inspect, and docker volume rm commands well, and you will be comfortable managing stateful Docker workloads.
<MCQExercise exercise={{ id: 'docker-named-volumes-q1', title: 'Question 1: Persistence', description: 'Why are named volumes commonly used with databases in Docker?', options: [ { id: 'a', text: 'Because they make the container image smaller' }, { id: 'b', text: 'Because the data stays available even if the database container is removed and recreated' }, { id: 'c', text: 'Because they disable networking for the container' }, { id: 'd', text: 'Because they force Docker to run only one container at a time' }, ], correctId: 'b', explanation: 'Databases need durable storage. Named volumes keep data outside the container lifecycle, so the container can be replaced without losing the database files.', wrongExplanations: { a: 'Volumes affect runtime storage, not the size of the image itself.', c: 'Volumes are about storage. They do not disable container networking.', d: 'You can run many containers with named volumes. Volumes do not limit container count.', }, }} />
<MCQExercise exercise={{ id: 'docker-named-volumes-q2', title: 'Question 2: Volume Inspection', description: 'Which command shows details such as the driver and mountpoint of a Docker volume?', options: [ { id: 'a', text: 'docker volume inspect myvolume' }, { id: 'b', text: 'docker volume show myvolume' }, { id: 'c', text: 'docker inspect-volume myvolume' }, { id: 'd', text: 'docker volume status myvolume' }, ], correctId: 'a', explanation: 'docker volume inspect prints structured information about the volume, including the driver, mountpoint, name, and scope.', wrongExplanations: { b: 'Docker does not provide a docker volume show command.', c: 'docker inspect-volume is not a valid Docker CLI command.', d: 'docker volume status is not the command used for detailed volume metadata.', }, }} />
<MCQExercise exercise={{ id: 'docker-named-volumes-q3', title: 'Question 3: Shared Use', description: 'What is usually the safest pattern when multiple containers access the same named volume?', options: [ { id: 'a', text: 'Many unrelated containers writing to the same files at the same time' }, { id: 'b', text: 'One writer and one or more readers when the workload allows it' }, { id: 'c', text: 'No container should ever mount a named volume read-only' }, { id: 'd', text: 'Every volume must be deleted after one container stops' }, ], correctId: 'b', explanation: 'One writer with additional readers is a safer design because concurrent writes to the same files can create corruption or consistency problems if the application does not coordinate access.', wrongExplanations: { a: 'Multiple uncoordinated writers are risky because the volume does not provide application-level locking.', c: 'Read-only mounts are often a good idea for readers and reduce accidental changes.', d: 'Named volumes are meant to persist beyond one container stop. Deleting them immediately defeats the purpose.', }, }} />
A named volume is managed by Docker and referenced by name; a bind mount maps a specific host directory into the container. Named volumes are better for persistent application data, a database's files, uploaded content, because they are portable and Docker manages permissions and location. Bind mounts are better for development, where you want to edit files on the host and see changes inside the container immediately. Bind mounts are also noticeably slower on macOS and Windows.
On Linux, under /var/lib/docker/volumes/<name>/_data. On macOS and Windows they live inside the Linux VM that Docker Desktop manages, so the path is not directly browsable from the host, which is deliberate, since the point of a named volume is that you do not manage its location. Use docker volume inspect <name> to see the mountpoint, and docker run --rm -v <name>:/data alpine ls /data to look inside.
Yes. That is their purpose. Removing a container leaves its named volumes intact, so recreating the container reattaches the same data. This also means they accumulate: docker container prune does not remove them, and neither does plain docker system prune without the --volumes flag. Run docker volume ls periodically, and docker volume prune to remove volumes no container references.
Run a throwaway container that mounts both the volume and a host directory, then archive one into the other: docker run --rm -v myvolume:/data -v $(pwd):/backup alpine tar czf /backup/backup.tar.gz -C /data .. Restoring reverses it. For a database, prefer the engine's own dump tool over a filesystem copy, since copying live database files can capture an inconsistent state.
Yes, and it is a common pattern. One container writes files and another reads them, or several replicas share a cache. Mount the same volume name in each. Be aware Docker provides no coordination: if two containers write to the same files concurrently you are responsible for avoiding corruption, exactly as you would be with two processes on a host. Most databases explicitly do not support multiple instances sharing one data directory.
Learn how to use a Docker bind mount with -v and --mount to share a host directory, make it read-only with :ro, and set up live reload for development.
Manage Docker volumes with docker volume ls, inspect, rm and prune. Learn how to back up a volume, restore it from a tar archive and migrate data between hosts.
Learn what Docker is and why developers use it. Understand images vs containers, how Docker differs from a virtual machine, the basic workflow and its limits.