Where Does Docker Store Data? Volumes vs Bind Mounts Explained
Delete a container and its files go with it, unless you stored them outside the container. That is the first surprise most Docker users hit: container storage is temporary by design. Volumes vs bind mounts are the two ways to keep data alive across container restarts, updates, and rebuilds, and picking the wrong one makes backups and moves harder than they need to be.
This guide shows where Docker puts each type, how to create and inspect them, and which one fits a database, a config file, and a folder you edit by hand. You need Docker installed and a container you can stop and start. Nothing here deletes data, but do not run the prune commands at the end on a host with data you have not backed up.
Where Docker Stores Data by Default
Inside a container, files live in the container’s writable layer. That layer exists only as long as the container does. When you run docker rm, the layer is deleted. Rebuilding an image does not carry it over either. Anything you want to keep, a database, uploaded files, or a config you edited, needs to live outside that layer.
Docker’s own storage area sits under /var/lib/docker/ on the host, and that directory is the heart of docker data storage on a host. That is the docker volume location for named volumes and the home of image layers, container layers, and internal metadata. You should not edit files directly inside /var/lib/docker/. Use Docker commands to inspect and back up volumes, because Docker tracks volume ownership and may store data in a layout you should not guess at.
Named Volumes: Docker-Managed Storage
For named volumes docker creates and manages the storage. You give it a name, Docker picks a directory under /var/lib/docker/volumes/, and you attach it to a container at a path like /var/lib/postgresql/data. The container sees normal files at that path. The host sees a Docker-managed directory you are not supposed to hand-edit.
docker volume create app-data docker run -d --name app -v app-data:/data my-app:latest
Named volumes have three practical strengths. First, they survive container deletion: remove the container and the volume stays until you delete it on purpose. Second, Docker can initialise a new named volume with the files already in the image at that path, which is handy for apps that ship default data. Third, they travel with Docker’s own tooling, so backup tools and move scripts can find them by name. The guide to backing up Docker volumes automatically builds on exactly this property.
Find a volume’s real path with inspect:
docker volume inspect app-data
The output shows a Mountpoint under /var/lib/docker/volumes/. Read it for diagnosis, not for daily editing. If you edit files there while the container runs, you can still break the app, and Docker will not warn you.
Bind Mounts: Your Own Folder, Mounted In
A bind mount maps a specific host path, one you choose, into the container. The container reads and writes the same files you see on the host. This is the right choice when you need to edit files by hand, when a config lives in your own directory structure, or when another tool on the host needs the same files.
A docker bind mount example looks like this:
docker run -d --name web -v /home/user/my-site:/usr/share/nginx/html nginx:latest
Note the difference from a named volume: the part before the colon is a full host path, not a name. If that host path does not exist, Docker creates an empty directory there, which has caused many empty-config mysteries: the app starts, finds an empty folder where its config should be, and writes defaults over nothing. Create the host folder and put the files in it before you start the container.
Permissions are the other common bind mount problem. Files inside the container are written with the container user’s ID, which may not match your host user. A container running as root can create files you cannot edit as a normal user, and a container running as an unprivileged user can fail to write to a folder owned by root. When a bind-mounted app reports permission denied, check the owner and mode on the host folder before you change the app.
Volumes vs Bind Mounts: Which One to Pick
| Question | Named volume | Bind mount |
|---|---|---|
| Who picks the host path? | Docker, under /var/lib/docker/volumes/ | You, any path on the host |
| Can you edit files by hand? | Possible but awkward | Yes, files are right there |
| Survives container deletion? | Yes, until you remove the volume | Yes, the host folder stays |
| Best for databases and app data? | Yes, this is the default choice | Works, but adds permission work |
| Best for config and code you edit? | Not ideal | Yes |
| Easy to move to another VPS? | Yes, with a volume backup or copy | Yes, copy the folder |
A simple rule covers most self-hosted setups: use named volumes for data the app owns, like databases, uploads, and caches, and use bind mounts for files you own, like config files and site content you edit. Mixing them in one stack is normal. A typical web app might use a bind mount for its nginx config and a named volume for its database, and that split is exactly what makes the stack easy to update: the config stays readable, and the data stays managed.
Anonymous volumes are a third case worth recognising. If a Dockerfile declares a VOLUME and you start the container without naming one, Docker creates an anonymous volume with a long random ID. It works like a named volume, but it is hard to find later and easy to orphan. Name your volumes on purpose so you never have to hunt through random IDs during a move.
Volumes in Compose and Moving Them
In Compose, declare named volumes in a top-level volumes block and attach them in each service. Bind mounts are just host paths in the same volumes list for a service, which is why the two look similar in a Compose file even though they behave differently on the host.
services:
db:
image: postgres:16
volumes:
- db-data:/var/lib/postgresql/data
web:
image: nginx:latest
volumes:
- ./site-config:/etc/nginx/conf.d
volumes:
db-data:
When you move a stack to another host, named volumes need a copy step: back up the volume, copy the archive, and restore it into a volume of the same name on the new host. The walkthrough for moving Docker containers to a new VPS covers that transfer in order. Bind mounts move with a plain folder copy, but check file owners after the copy, since user IDs may differ on the new host.
Two cleanup commands deserve care. docker volume prune deletes every volume not attached to a container, including volumes from containers you removed and plan to recreate. docker system prune --volumes goes further and removes unused containers, networks, and images too. Both ask for confirmation. Before you accept, list volumes with docker volume ls and confirm that nothing in the list belongs to a stopped container you still need. If you want a safer backup setup before any cleanup, compare Duplicati vs Restic vs Kopia for Docker volumes and pick a tool that fits your restore habits. For a first Compose file that uses both storage types, the walkthrough in Docker Compose explained through your first file shows the syntax in context.
Check your own stack now: run docker inspect on each container, look at the Mounts section, and confirm every path that must survive a rebuild is a named volume or a bind mount, not container storage. Any important path still in container storage is one docker rm away from disappearing.