Volumes and Data Persistence

Understand why container data is lost and learn to preserve it with bind mounts and volumes.

🎯 Introduction

By the end of this article, you will understand why removing a container removes its data, and learn two ways to preserve data with Docker: bind mounts and volumes. You can then confidently run software such as MySQL that needs persistent storage, a prerequisite for the database courses.


🧱 Why Container Data Is Lost

The previous article emphasized that removing a container removes its data. This happens because files written inside a container are stored in its own filesystem. Think of a container as a disposable box: throw away the box, and its contents go with it.

Starting over is fine when learning, but not in a real application. If you run MySQL with Docker, removing a container must not erase all your business data. We need to store data outside the container so it survives independently. This is data persistence.


🧱 What Is a Volume?

Docker provides volumes for persistent storage. Think of a USB drive:

  • A container is like a computer: it can fail or be replaced.
  • A volume is like a USB drive: its data stays available when you connect it to another computer (container).

When a container starts, you mount a volume at a directory inside it. Reading and writing that directory actually accesses the volume outside the container. The data therefore survives even when the container is removed.

Two common ways to mount storage in Docker are bind mounts and named volumes. Let's look at each.


🛠 Option 1: Bind Mounts

A bind mount maps a directory on your computer to a directory inside a container. Specify it with -v HOST_PATH:CONTAINER_PATH.

For example, let Nginx serve a page you wrote. First, create a directory and page on your computer:

mkdir ~/my-web
echo '<h1>Hello Docker</h1>' > ~/my-web/index.html

Then start a container, mounting that directory where Nginx stores its web pages:

docker run -d --name web -p 8080:80 -v ~/my-web:/usr/share/nginx/html nginx

Open http://localhost:8080 to see your “Hello Docker” page. Edit ~/my-web/index.html and refresh the browser to see the changes without restarting the container.

Bind mounts are common during development: edit code on your computer with your editor, and the container runs those same files. Changes are available immediately.

🛠 Option 2: Named Volumes

A named volume is storage managed by Docker. Give it a name; you do not need to manage its location on disk.

For example, MySQL stores its data in /var/lib/mysql inside the container. The MySQL course uses this in more detail. Mount a volume named mysql-data there:

docker run --name mysql8 -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -v mysql-data:/var/lib/mysql -d mysql:8

Docker creates mysql-data automatically if it does not exist. If you later remove the stopped container with docker rm mysql8, the data remains in mysql-data. Run the same command again to mount the volume in a new container and recover the existing data.

List and manage volumes:

docker volume ls
docker volume rm mysql-data
Explicitly removing a volume with docker volume rm deletes its data, so check before proceeding. Removing a container with docker rm does not automatically remove its named volumes.

⚡ Which Option Should You Choose?

  • Bind mounts: Use these when you need to inspect or edit files directly, such as source code and configuration files. Your changes are available immediately.
  • Named volumes: Use these when software manages its own files and you do not need direct access, such as database data files. Let Docker manage the storage.

A useful rule: use bind mounts for files you work with directly, and volumes for data managed by programs.


❓ Questions and Answers

Q1: What happens to existing files at the mount location?

The mounted contents hide them. For example, mounting an empty ~/my-web over the Nginx web directory hides the bundled welcome page; Nginx sees the mounted directory's contents instead.

Q2: Where are named volume files actually stored?

Docker stores them in a directory it manages, inside the Docker Desktop environment. You generally do not need the exact location; refer to the volume by name.

Q3: Can multiple containers share a volume?

Yes. Containers mounting the same volume can share its data. However, programs writing the same data concurrently can interfere with each other. Generally, avoid this for database software.

Q4: Can I skip volumes while learning?

Yes. Recreating lost practice data can be convenient. Just understand the distinction: without persistent storage, removing the container loses its data; mounting a volume preserves the data stored there.

Q5: Can I use a relative path for a bind mount?

Prefer an absolute path, such as ~/my-web (expanded by your shell) or /Users/your-username/my-web. Relative paths depend on where you run the command, making it easy to mount the wrong directory without an obvious error and difficult to troubleshoot.


🧪 Practice

  1. Run Nginx with a bind mount to serve your own page. Edit the page and refresh your browser to verify that changes appear immediately:
# Hint: create a directory with mkdir, write index.html with echo, then mount it with docker run
# Write your commands here
  1. Run MySQL with a named volume, open its client, and create a database. Then remove the container and run it again. Open the client and check whether the database remains to verify persistence:
# Hint: use this article's MySQL command, then open the client with docker exec -it mysql8 mysql -uroot -p
# SQL to create the database: CREATE DATABASE demo;
# Write your commands here

🎉 You now know two ways to persist data! Next, we will use a Dockerfile to package your own Node.js application into an image.