Skip to main content
Docker Basics Tutorial
CHAPTER 06 Beginner

Docker Volumes and Persistent Storage

Updated: May 15, 2026
25 min read

# CHAPTER 6

Docker Volumes and Persistent Storage

1. Introduction

The most critical rule of Docker is that Containers are Ephemeral. This means they are temporary. When a container is deleted, everything inside its internal file system is instantly and permanently destroyed. This is fantastic for a stateless web server, but it is an absolute catastrophe for a database. If your MySQL container crashes and is recreated, you lose every user and password in your system! To solve this, Docker provides Volumes and Bind Mounts to persist data safely outside the container.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the ephemeral nature of container file systems.
  • Define what a Docker Volume is and how it persists data.
  • Differentiate between Named Volumes and Bind Mounts.
  • Mount a local directory into a container for live development.
  • Persist database records using a Named Volume.

3. Beginner-Friendly Explanation

Imagine staying in a hotel room (The Container).
  • Without Volumes: You buy groceries and put them in the hotel room's mini-fridge. When your stay is over, you check out, the cleaning staff wipes the room completely clean, and your groceries are thrown in the trash.
  • Bind Mount: You bring a magical portal backpack. You attach the portal to the hotel mini-fridge. The portal connects directly to your refrigerator back at your actual house. Anything you put in the hotel fridge is instantly teleported to your home. When you check out of the hotel, the food is safe at your house!
  • Named Volume: You rent a permanent, locked storage locker down in the hotel basement. When you check out of Room 101, the locker remains safe. Next week, you check into Room 505 and ask the front desk to connect your room to that exact same storage locker.

4. Bind Mounts (For Live Development)

A Bind Mount maps a specific, absolute folder on your laptop (e.g., C:\my-project) directly to a folder inside the container (e.g., /var/www/html). The Use Case: Live Development. If you use a Bind Mount, you can open VS Code on your laptop, edit your index.html file, hit save, and the container instantly sees the updated file without needing to rebuild the image!

The CLI Command (using -v):

bash
1
docker run -v /Users/john/my-project:/var/www/html nginx

*(Format is -v <Host_Laptop_Path>:<Container_Internal_Path>)*

5. Named Volumes (For Production Databases)

A Named Volume is a storage folder completely managed by Docker. You do not know or care exactly where the folder is on your hard drive; you just give it a name (like db-data). Docker protects it. The Use Case: Databases and Production Storage. If you launch a MySQL container, you attach a Named Volume to its internal data folder. If the container is deleted, the db-data volume survives safely on Docker's hidden hard drive space.

The CLI Command:

bash
1
docker run -v db-data:/var/lib/mysql mysql

6. Mini Project: Persist MySQL Data

Let's prove that volumes can survive a catastrophic container deletion.

Step-by-Step Tutorial:

  1. 1. Open your terminal. Let's run a MySQL container and attach a Named Volume to its critical data folder.

bash
12345
docker run -d \
  --name my-database \
  -e MYSQL_ROOT_PASSWORD=secret \
  -v mysql-data:/var/lib/mysql \
  mysql:8.0

*(Notice the -e flag sets an Environment Variable for the password, and -v creates the Named Volume).*

  1. 2. Log into the database container:

bash
1
docker exec -it my-database mysql -u root -p

*(Type secret when prompted for the password).*

  1. 3. Inside the MySQL prompt, create some data:

sql
123
CREATE DATABASE testdb;
SHOW DATABASES;
exit
  1. 4. The Catastrophe: Let's completely destroy the container!
bash
12
docker stop my-database
docker rm my-database

*Oh no! Is testdb gone?*

  1. 5. Let's launch a brand new, completely fresh MySQL container, but attach the *exact same* Volume name:

bash
12345
docker run -d \
  --name new-database \
  -e MYSQL_ROOT_PASSWORD=secret \
  -v mysql-data:/var/lib/mysql \
  mysql:8.0
  1. 6. Log into the new database and check:
bash
1
docker exec -it new-database mysql -u root -p

Type SHOW DATABASES;. You will see testdb is still there! The Named Volume successfully shielded your data from the container's destruction.

7. Managing Volumes

Just like containers, volumes can pile up and waste disk space.
  • List Volumes: docker volume ls
  • Inspect Volume: docker volume inspect mysql-data
  • Delete Volume: docker volume rm mysql-data (Warning: This permanently deletes your data!)

8. Best Practices

  • Never Use Bind Mounts for Databases in Production: Bind mounts rely on the specific file system and permission structure of the host operating system (Windows vs Mac vs Linux). This causes massive permission errors with databases. Always use Docker-managed Named Volumes for database persistence.

9. Common Mistakes

  • The Empty Folder Override: If you create a Bind Mount mapping an *empty* folder on your laptop to a folder inside the container that already contains files, your laptop's empty folder will "overwrite" and hide the container's files! This often causes containers to crash on startup because they suddenly can't find their own configuration files.

10. Exercises

  1. 1. Contrast a Bind Mount with a Named Volume. In what scenario is a Bind Mount highly preferred?
  1. 2. If a Docker container running PostgreSQL is forcefully removed (docker rm -f), what happens to the data if the container was utilizing a Named Volume?

11. FAQs

Q: Where does Docker physically save Named Volumes on my computer? A: On Linux, they are usually located in /var/lib/docker/volumes/. On Windows and Mac, because Docker runs inside a hidden Linux micro-VM, you cannot easily navigate to the volume folders using your standard File Explorer. Docker deliberately hides them to protect them from accidental user tampering!

12. Interview Questions

  • Q: Explain the architectural necessity of Docker Volumes. Provide a scenario demonstrating how deploying a stateful application (like a database) without a volume would result in critical failure.
  • Q: A developer wants to see live changes to their HTML/CSS code reflected immediately in their browser without having to run docker build every time they click save. How would you configure their container to achieve this workflow?

13. Summary

In Chapter 6, we solved the problem of ephemerality. We learned that while containers are temporary and disposable, critical data must be permanent. We explored Bind Mounts, creating a live portal between our host operating system and the container to dramatically speed up local development. We then utilized Named Volumes to architect a resilient database, proving that by detaching the storage lifecycle from the container lifecycle, our data can survive the complete destruction of the application.

14. Next Chapter Recommendation

Our data is safe, but our containers are currently isolated islands. A web server container needs to talk to a database container. Proceed to Chapter 7: Docker Networking Basics.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·