Skip to main content

Command Palette

Search for a command to run...

Docker Volumes Explained with MySQL Example

Published
4 min read
Docker Volumes Explained with MySQL Example
A

🚀 𝐀𝐛𝐨𝐮𝐭 𝐌𝐞 "Hi, I'm Ali masiu zama, a DevOps student passionate about cloud computing and automation. I'm currently learning AWS, Linux, Docker, and CI/CD pipelines, with a focus on automating workflows and building scalable solutions.

If you’ve ever deleted a Docker container and lost your database data — don’t worry, it’s a common mistake!
In this post, we’ll explore how Docker Volumes can help persist your data even after containers are removed.

Let’s go through a practical example using MySQL on Docker.

Table of Contents

  1. Introduction to Docker Volumes

  2. Step 1 — Create Directory for MySQL Volume

  3. Step 2 — Run MySQL Container with Volume Mount

  4. Step 3 — Access MySQL and Create Database

  5. Step 4 — Insert Data into the Database

  6. Step 5 — Delete Container and Test Data Persistence

  7. Step 6 — Recreate Container and Verify Stored Data

  8. Why Docker Volumes Are Important

What is a Docker Volume?

A Docker Volume is a storage mechanism that allows data to be stored outside the container’s writable layer, ensuring that data remains even after containers are deleted or recreated.

It’s the preferred way to handle persistent data in Docker — especially for databases like MySQL or PostgreSQL.

Step 1: Create a Directory for the Volume

We’ll first create a local directory that will store MySQL data permanently.

mkdir mysql-volume
cd mysql-volume
pwd

The output shows your path (for example):

/home/ubuntu/mysql-volume

Step 2: Run a MySQL Container with Volume Mount

Now, let’s run a MySQL container and mount our created directory using the -v flag.

docker run -d -v /home/ubuntu/mysql-volume:/var/lib/mysql --name mysql -e MYSQL_ROOT_PASSWORD=root
mysql:latest

Explanation:

OptionDescription
-dRun container in detached mode
-vMounts our local folder to the container path /var/lib/mysql
--nameAssigns a name to our container (mysql)
-e MYSQL_ROOT_PASSWORD=rootSets the root password for MySQL
mysql:latestImage used to run the container

Step 3: Access the MySQL Container

List running containers:

docker ps

Example output:

CONTAINER IDIMAGESTATUSNAMES
2467d65eab12mysql:latestUp 10 secondsmysql

Now enter into the container:

docker exec -it 2467d65eab12 mysql -u root -p

Enter the password (root), and you’ll enter the MySQL shell.

Step 4: Create Database and Table

Now let’s create a database and table inside MySQL.

show databases;

create database college;

use college;

create table students(
  id int,
  name varchar(50)
);

insert into students values(101, "Ankit");

select * from students;

Output:

idname
101Ankit

Step 5: Delete the Container

Let’s now delete the container to test if our data is really persisted.

docker ps
docker stop 2be
docker rm 2be
docker ps

Output:

CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

No containers are running — meaning the MySQL container is fully deleted.

Step 6: Recreate the Container

Now, create a new MySQL container with the same volume mount path:

docker run -d -v /home/ubuntu/mysql-volume:/var/lib/mysql --name mysql -e MYSQL_ROOT_PASSWORD=root
mysql:latest

Then access MySQL again:

docker exec -it <new-container-id> mysql -u root -p

Now check your databases:

show databases;
use college;
select * from students;

Output:

idname
101Ankit

Your data is still there!
That’s the magic of Docker Volumes — even after deleting containers, data remains safe.

Why Docker Volumes Are Important

BenefitDescription
PersistenceData survives container removal or crashes
PortabilityEasy to back up or move to another host
PerformanceOptimized for Docker engine’s file system
SharingCan be shared between multiple containers

Conclusion !!!

Using Docker Volumes is one of the most reliable ways to handle persistent storage in containerized environments.
This simple MySQL demo proves that even if containers are deleted, your data remains safe.

More from this blog

DevOpsAlimasiuzama

30 posts