Easy Migrated MySQL from System to Docker with Data and Volume Storage 2025

Migrated MySQL from EC2 System to Docker with Data and Volume Storage

Migrated a working MySQL setup (installed directly on an EC2 server) into a Docker container – without losing any data. This was a real-time exercise done on a RHEL 9 EC2 instance, and it helped me understand how Docker works with database storage and volumes.

Migrated MySQL from System to Docker Prerequisites:

  • AWS EC2 Instance (RHEL 9 or any Linux):
  • Sudo or Root Access:
  • MySQL Installed on the System:
  • Docker Installed:
  • MySQL Docker Image (Same Version):
  • Basic Linux & SQL Knowledge:

Migrated MySQL from System to Docker Flowchart

Migrated MySQL from System to Docker
ALSO READ:

Reusable Terraform Modules Made My AWS EC2 Setup Super Easy
Effortless AWS EC2 Deployment with Terraform: Automate Your Infrastructure Today!
Best Kubernetes Services Explained: ClusterIP, NodePort, and LoadBalancer 2025

Migrated Installed and Tested MySQL on system

sudo systemctl start mysqld
mysql -u root -p

Then I created:

  • A database: techbasehub_db
  • Two tables: users and projects
  • Inserted real user and project data

Migrated Set up MySQL on the system with a database, Tables, and data.

-- Create Database
CREATE DATABASE IF NOT EXISTS techbasehub_db;

-- Use the database
USE techbasehub_db;

-- Create a professional table for users
CREATE TABLE IF NOT EXISTS users (
    user_id INT AUTO_INCREMENT PRIMARY KEY,
    full_name VARCHAR(100) NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    role VARCHAR(50) DEFAULT 'viewer',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create a table for projects
CREATE TABLE IF NOT EXISTS projects (
    project_id INT AUTO_INCREMENT PRIMARY KEY,
    project_name VARCHAR(100) NOT NULL,
    description TEXT,
    status ENUM('active', 'completed', 'on-hold') DEFAULT 'active',
    owner_id INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (owner_id) REFERENCES users(user_id)
);

-- Insert sample users
INSERT INTO users (full_name, email, role)
VALUES 
('Krishna T', 'krishna@techbasehub.com', 'admin'),
('Ananya Roy', 'ananya@techbasehub.com', 'developer');

-- Insert sample projects
INSERT INTO projects (project_name, description, status, owner_id)
VALUES 
('Cloud Infra Setup', 'Terraform project for AWS infra', 'active', 1),
('Docker Workshop', 'Internal training setup using Docker', 'completed', 2);

Migrated Pulled the Same MySQL Image in Docker

After confirming that data and queries were working, I stopped the MySQL system.

sudo systemctl stop mysqld
sudo systemctl disable mysqld

Then pulled the Docker image:

docker pull mysql:8.0.41

Migrated Ran MySQL in Docker with Volume Mount

docker run -d   --name my-mysql -p 3306:3306 -v /var/lib/mysql:/var/lib/mysql  mysql:8.0.41

Migrated Verified Data Inside the Docker Container

docker exec -it my-mysql bash
mysql -u root -proot@123

Then I ran the same queries and confirmed worked perfectly

[devuser@dev-devops ~]$ docker exec -it my-mysql bash
bash-5.1# mysql -u root -proot@123


mysql> USE techbasehub_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> SELECT * FROM users;
+---------+------------+-------------------------+-----------+---------------------+
| user_id | full_name  | email                   | role      | created_at          |
+---------+------------+-------------------------+-----------+---------------------+
|       1 | Krishna T  | krishna@techbasehub.com | admin     | 2025-06-28 15:32:04 |
|       2 | Ananya Roy | ananya@techbasehub.com  | developer | 2025-06-28 15:32:04 |
+---------+------------+-------------------------+-----------+---------------------+
2 rows in set (0.00 sec)

mysql> SELECT
    ->     p.project_id,
    ->     p.project_name,
    ->     p.status,
    ->     u.full_name AS owner_name,
    ->     p.created_at
    -> FROM
    ->     projects p
    -> JOIN
    ->     users u ON p.owner_id = u.user_id;
+------------+-------------------+-----------+------------+---------------------+
| project_id | project_name      | status    | owner_name | created_at          |
+------------+-------------------+-----------+------------+---------------------+
|          1 | Cloud Infra Setup | active    | Krishna T  | 2025-06-28 15:32:11 |
|          2 | Docker Workshop   | completed | Ananya Roy | 2025-06-28 15:32:11 |
+------------+-------------------+-----------+------------+---------------------+
2 rows in set (0.00 sec)

mysql> exit

Thank You

Click here to go GitHub repos link

Leave a Comment

Index