DEV Community

Cover image for Docker Volumes Explained: Keep Your Database Data Safe
Shuvo Halder
Shuvo Halder

Posted on Edited on

Docker Volumes Explained: Keep Your Database Data Safe

Docker volumes explained — why data disappears when a container stops, and how named volumes keep your database safe

Your data disappears when your Docker container stops. You restart a database container, the app comes back up, and the data is gone. I learned this the hard way with a test database. The app was fine. The data was not.

The reason is simple, and it is by design. A Docker container is temporary. When you stop it and remove it, the files inside it go away too. That is fine for code. It is bad for a database. Docker volumes fix this. In this post I will show you how Docker volumes work, the difference between named volumes and bind mounts, and the exact commands I use to keep database data safe.

What is a Docker volume?

A Docker volume is storage that lives outside the container. The container can stop, start, or be deleted, and the volume stays. When you start a new container with the same volume, your data is still there.

Think of it this way: the container is the worker, and the volume is the filing cabinet. You can replace the worker. You do not throw away the filing cabinet with them.

You can read the official reference in the Docker volumes documentation. Docker keeps three kinds of storage separate: volumes, bind mounts, and tmpfs mounts. For database data, volumes are the right choice in almost every case.

No volume: data dies with the container

This is the mistake most people make first. You run a database in Docker with no volume at all:

docker run -d --name mydb mysql:8
Enter fullscreen mode Exit fullscreen mode

It works. You add data, you test the app, everything looks fine. Then you stop the container and remove it:

docker stop mydb
docker rm mydb
Enter fullscreen mode Exit fullscreen mode

Now the data is gone. Docker does not save it for you. The writable layer of the container is deleted with the container. There is no recycle bin, and there is no warning that will save you after the fact.

This is the most common way people lose a Docker database. It works until the first restart. Then they lose everything, and they learn about volumes the hard way. I do not want that to be you.

Named volumes: data stays safe

A named volume is the safest way to store database data in Docker. You create it once, and Docker manages where the files live on the host.

docker volume create my-data
docker run -d -v my-data:/var/lib/mysql --name mydb mysql:8
Enter fullscreen mode Exit fullscreen mode

Now the MySQL data lives in the my-data volume, not inside the container. You can stop the container, remove it, and start a new one with the same volume:

docker stop mydb
docker rm mydb
docker run -d -v my-data:/var/lib/mysql --name mydb mysql:8
Enter fullscreen mode Exit fullscreen mode

Your data is still there. The container was replaced. The volume was not.

Use named volumes for databases. Always. This is the rule I follow on every server I manage, local or production. If the data matters, it goes in a named volume.

You can see all your volumes, inspect one, and back one up like this:

docker volume ls
docker volume inspect my-data
docker volume rm my-data
Enter fullscreen mode Exit fullscreen mode

Only run docker volume rm when you are sure. Deleting a volume deletes the data in it. There is no undo. I check twice before I remove a volume, and I never remove one while a container is still using it.

Bind mounts: data lives in a folder you pick

A bind mount points Docker to a folder on your host that you choose. You can see the files with your own eyes, edit them directly, and back them up with your normal backup tools.

docker run -d -v /home/app/data:/data --name myapp my-app
Enter fullscreen mode Exit fullscreen mode

Here /home/app/data is a folder on the host, and /data is where the container sees it. Whatever the container writes to /data shows up in that host folder right away.

Bind mounts are good for config files, local development, and cases where you need direct access to the files. They are not my first choice for a production database, because the files depend on that exact host path. If you move the container to another server, the path may not exist there. A named volume moves with Docker. A bind mount does not.

That is the main difference:

Docker storage compared: no volume loses data, named volume keeps data safe, bind mount uses a host folder

  • Named volume: Docker manages the location. Portable, safe for databases, easy to back up and move.
  • Bind mount: you pick the folder. Good for config and local testing, where you want to see and edit the files directly.
  • No volume: data lives inside the container only. It dies with the container. Never use this for a database.

Docker volumes in Compose

Most real projects use Docker Compose, and volumes are even easier there. You declare the volume once, and Compose creates it for you.

services:
  db:
    image: mysql:8
    environment:
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - db-data:/var/lib/mysql

volumes:
  db-data:
Enter fullscreen mode Exit fullscreen mode

That one block under volumes: is what saves your database. Without it, every docker compose down that removes the container can take your data with it. With it, the data survives restarts, rebuilds, and container replacements.

You can read more about this in the Docker Compose documentation. The pattern is the same in Kubernetes too, where volumes are called PersistentVolumes. The idea does not change: keep the data outside the thing that can be deleted.

How I check a volume before I trust it

Before I trust a volume with real data, I check three things.

1. Is the volume actually attached?

docker inspect mydb --format '{{ .Mounts }}'
Enter fullscreen mode Exit fullscreen mode

This shows what is mounted and where. If the output is empty, the container has no volume, and the data is at risk.

2. Does the data survive a restart?

I test this on purpose, with test data first. Stop the container, remove it, start a new one with the same volume, and check that the data is still there. If it is not, I find out now, with test data, not later with real data.

docker stop mydb && docker rm mydb
docker run -d -v my-data:/var/lib/mysql --name mydb mysql:8
docker exec mydb mysql -uroot -psecret -e "SHOW DATABASES;"
Enter fullscreen mode Exit fullscreen mode

3. Can I back it up?

A volume is not a backup. It protects you from a container being deleted. It does not protect you from the host disk failing, or from someone deleting the volume by mistake. I back up database volumes on a schedule, and I test the restore. A backup you have never restored is a hope, not a backup.

For MySQL, a simple logical backup looks like this:

docker exec mydb mysqldump -uroot -psecret --all-databases > backup.sql
Enter fullscreen mode Exit fullscreen mode

Store that file somewhere else, not on the same host. That is the part people skip, and it is the part that matters when the host itself fails.

Common Docker volume mistakes

These are the mistakes I see most, and the ones that cost people their data.

Storing database data inside the container. This is the number one mistake. It works until the first restart. Put database data in a named volume from day one.

Using a bind mount for a database on the wrong path. The container writes to a host folder that does not exist, or that has the wrong permissions. The database fails to start, and the error looks like a database problem. Check the host path and the permissions first.

Deleting a volume to "clean up". docker volume prune and docker volume rm delete data. They do not ask twice. I never run a prune on a server with data I care about unless I have checked what it will remove first.

Forgetting the volume in Compose. You write the service, you forget the volumes: block, and everything works in testing. Then you run docker compose down -v, and the -v flag removes the volumes too. That flag is the dangerous one. I read the command twice before I use it.

No backup, only a volume. A volume keeps data safe when a container is replaced. It does not keep data safe when the disk dies. Back up the volume, and keep the backup off the host.

When to use each type of Docker storage

Here is the short version I use when I set up a new project:

  • Database data: named volume. Always. Postgres, MySQL, MongoDB, it does not matter which one. Named volume.
  • Config files you edit often: bind mount. You want to see the file, edit it on the host, and restart the container.
  • Local development code: bind mount. Your code lives on the host, the container runs it, and you see changes right away.
  • Temporary data, caches, logs you can lose: no volume, or a tmpfs mount. If it disappears, nothing breaks.
  • Data that must survive a container rebuild: named volume, plus a backup schedule.

If you are not sure, use a named volume. It is the safe default, and you can change it later.

Why this matters for remote teams

When I work with remote teams, volumes are part of the setup I check first. A developer runs the project locally with Compose, the database works, and then someone runs docker compose down -v and the test data is gone. The team loses an hour setting it up again. Multiply that by a team, and it is a real cost.

The fix is boring, and that is the point. One named volume in the Compose file, one backup check, and the problem goes away. I write it into the project README so the next person does not have to learn it the hard way.

The same idea applies to the systems I run in production. I run GenieACS on k3s for an ISP with almost 30,000 devices, and the same rule holds there: state lives outside the thing that can be restarted. You can read how I handle that at scale in my GenieACS on k3s article. If you automate your server setup, the same thinking applies to virtual hosts too — I wrote about that in my Apache and Nginx virtual hosts article.

FAQ: Docker volumes

What is a Docker volume?
A Docker volume is storage that lives outside a container. The container can stop or be deleted, and the volume and its data stay. You attach it with -v my-data:/path or in a Compose file.

What is the difference between a named volume and a bind mount?
A named volume is managed by Docker — you give it a name, Docker picks the location. A bind mount is a folder you pick on the host. Named volumes are best for databases. Bind mounts are best for config files and local code.

Does data survive a container restart with a volume?
Yes. With a named volume, the data survives stop, start, remove, and replace of the container. Without a volume, the data is deleted with the container.

Does docker compose down -v delete my data?
Yes. The -v flag removes named volumes declared in the Compose file. Use docker compose down without -v if you want to keep the data, and be careful with prune commands.

How do I back up a Docker volume?
Back up the database from inside the container, for example with mysqldump, and store the backup off the host. A volume alone is not a backup — it does not protect you if the host disk fails.

Final thoughts

Build once. Run it everywhere. But keep your data in a volume.

A container is temporary by design. That is what makes Docker fast and easy to replace. It is also what makes it dangerous for data, if you forget that the container is not the place to store anything you care about.

One named volume, one line in your Compose file, and your database survives the restart. That is a small habit that saves a lot of pain later.

Try it on your next project. Run a database with a named volume, stop the container, remove it, start a new one, and check that your data is still there. Once you see it work, you will never run a database without a volume again.

If this helped, let me know in the comments. What did you lose the first time a container was deleted — a test database, or something worse?

More from me: my DEV profile.

Top comments (0)