Docker volumes, bind mounts, and why persistence matters

A container is ephemeral by design: remove it and its filesystem disappears along with it. Docker volumes solve this by separating data from the container’s lifecycle, a named volume lives independently, managed by Docker itself, until you explicitly delete it with docker volume rm. In this lab we prove it by writing a file from one container, removing that container entirely, and reading the same file back from a brand-new one, no tricks involved.

Three ways to keep data

Docker offers three mechanisms with very different behavior. The named volume lives under Docker’s own control in /var/lib/docker/volumes/ and is referenced by name (-v labdata:/data). The anonymous volume is the same mechanism with no name assigned: Docker gives it a random identifier that is easy to lose track of. The bind mount is different from the ground up, it mounts a path that already exists on the host (-v /host/path:/data) and creates no object Docker manages at all: the container sees literally the same file the host does.

-v versus –mount

The short -v source:target syntax is convenient but ambiguous: get the volume name wrong and Docker silently creates a new one instead of warning you. --mount type=volume,source=labdata,target=/data is more verbose but explicit, it splits type, source and target into key-value pairs and fails with a clear error when something doesn’t add up, which makes it the safer choice for production scripts.

If you already use Docker to self-host services, this same persistent-data pattern shows up almost everywhere: in our Gitea on Docker guide the volume holds the repositories, in the Vaultwarden one it holds the encrypted password vault, and in the Nginx Proxy Manager one it holds the certificates. You can browse the rest of our guides in the Tools category. If you also run Kubernetes, the volume concept has its own equivalent with different rules, and this DigitalOcean guide goes deeper into sharing data between several containers.

FAQ

What is the difference between a named volume and an anonymous one?

Both are real volumes managed by Docker and live in the same place on the host, but the anonymous one gets a random identifier Docker assigns when you create it with -v /path and no name before the colon, while the named one you create explicitly with docker volume create and can reference by that name from any container.

Where does a Docker volume physically store its data?

On the host running the Docker engine, under /var/lib/docker/volumes/<name>/_data. docker volume inspect shows that exact path in the Mountpoint field; you normally don't need to touch it by hand, but knowing it exists helps explain why the volume survives even after you remove the container.

Does docker rm delete the named volume a container was using?

No. docker rm -v only deletes the anonymous volumes tied to that specific container; a named volume is never removed as a side effect of deleting a container, no matter which flags you use. Removing it requires an explicit docker volume rm, which is what we do in the lab's final cleanup.