# Docker Volumes deep concept

# 🚀 Docker Advanced Practice: Deep Dive into Docker Volumes

In today's DevOps practice, I took a deep dive into Docker Volumes. This session focused on using volumes to create persistent storage for Docker containers, specifically for a MySQL database. Here’s a step-by-step breakdown of the process along with commands and images to illustrate each phase.

---

## 🏷️ Docker Volumes: Essential for Persistent Data Storage

### What are Docker Volumes?

Docker Volumes enable data persistence by storing container data on the host machine. This becomes crucial when running databases like MySQL, where data loss on container deletion or restart would be a major setback.

---

## 1️⃣ **Creating a Volume in a Directory**

In this section, I created a Docker volume in a specified directory and configured MySQL to use it, ensuring that MySQL data persists outside the container lifecycle.

### Step-by-Step Implementation

1. **Setting Up a Directory**
    
    * Created a directory named `mysql-data` on the host and obtained its path using `pwd`:
        
    
    ```bash
    mkdir mysql-data && cd mysql-data
    pwd
    ```
    
2. **Running a MySQL Docker Container with Volume Mounting**
    
    * Pulled the MySQL image and ran it while mounting the `mysql-data` directory:
        
    
    ```bash
    docker run -d -v /home/ubuntu/docker-advanced/mysql-data:/var/lib/mysql --name mysql -e MYSQL_ROOT_PASSWORD=test@123 mysql:latest
    ```
    
    * Note: MySQL requires the `MYSQL_ROOT_PASSWORD` environment variable for authentication.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729959054373/eb636cdf-cb15-44ae-88f9-5a6fcc609a24.png align="center")
        
3. **Verifying Container Creation**
    
    * Checked running containers with:
        
    
    ```bash
    docker ps
    ```
    
4. **Accessing MySQL in the Container**
    
    * Accessed the container’s shell and launched MySQL in interactive mode:
        
    
    ```bash
    docker exec -it <container_id> bash
    mysql -u root -p
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729959124545/d07981f5-a64c-4222-837a-622191eadfcc.png align="center")
    
5. **Creating a Database and Table in MySQL**
    
    * Checked initial databases, created a new database `mydb`, and added a table `names`:
        
    
    ```sql
    CREATE DATABASE mydb;
    USE mydb;
    CREATE TABLE names (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL);
    INSERT INTO names (name) VALUES ('Shubham Londhe'), ('Dheeraj Yadav'), ('Amitabh Soni');
    SELECT * FROM names;
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729959167673/72df2d82-4b13-4567-89bd-da15ef0b1ae3.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729959178642/15fb856f-9ede-4ec2-b6d5-ea4aa36f4350.png align="center")
    
6. **Confirming Volume Data**
    
    * After stopping the container, navigated to `mysql-data` and confirmed the database files:
        
    
    ```bash
    cd mysql-data
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729959237458/b7fb86ab-ced3-4d3e-8d29-8131646f1bf7.png align="center")
    
7. **Restarting Container**
    
    * Restarted and verified the data:
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729959342697/0f487775-35e3-44d6-9b7d-4dd9f48e4985.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729959395620/6c213d93-59e1-4ca1-8535-12947b6e0469.png align="center")
        
8. **Deleting Volume**
    
    * Removed the volume with elevated permissions:
        
    
    ```bash
    sudo rm -rf mysql-data
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729959422648/9fe5c45d-1f72-4006-a0b3-4a87515c3fcb.png align="center")
    

---

## 2️⃣ **Creating a Volume Using the Docker Volume Command**

Next, I explored creating a volume directly with Docker’s volume command.

### Step-by-Step Implementation

1. **Checking and Creating a Volume**
    
    * Listed existing volumes, created `mysql-data`, and inspected it for path details:
        
    
    ```bash
    docker volume ls
    docker volume create mysql-data
    docker volume inspect mysql-data
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729959461341/386b5700-9b82-40f6-96e1-2910c529b9b6.png align="center")
    
2. **Running MySQL with the New Volume**
    
    * Mounted `mysql-data` volume for MySQL data persistence:
        
    
    ```bash
    docker run -d -v mysql-data:/var/lib/mysql --name mysql -e MYSQL_ROOT_PASSWORD=test@123 mysql:latest
    docker exec -it <container_id> bash
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729959483092/4b48529f-e73e-410c-b25b-f5b459bc85c1.png align="center")
    
3. **Creating Database and Table for Volume Test**
    
    * Created `rank_list` database, `rankers_table`, and inserted data for testing:
        
    
    ```sql
    CREATE DATABASE rank_list;
    USE rank_list;
    CREATE TABLE rankers_table (id INT AUTO_INCREMENT PRIMARY KEY, rankers VARCHAR(100) NOT NULL);
    INSERT INTO rankers_table (rankers) VALUES ('Shubham Londhe'), ('Dheeraj Yadav'), ('Amitabh Soni');
    SELECT * FROM rankers_table;
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729959493962/504dba8b-9002-4780-ba54-7a9c544a629e.png align="center")
    
4. **Verifying Volume Data**
    
    * Located `rank_list` in the specified volume path:
        
    
    ```bash
    sudo su
    cd /var/lib/docker/volumes/mysql-data/_data
    ls
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729959506138/a35ea542-b0b9-46db-98a7-49f466367487.png align="center")
    
5. **Restarting and Re-verifying Data**
    
    * Stopped, removed, and re-ran the container to ensure data persistence.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729959530679/cc9d731d-d515-4225-a00b-b91354b8fb5b.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729959536221/be7c94e3-3e3c-48ee-9b09-c86f9cdaf1e6.png align="center")
        

---

### Conclusion

Docker Volumes provide a powerful way to persist data between container sessions, crucial for applications like databases. Today’s hands-on with MySQL illustrated the concept effectively, showing how volumes help in creating durable data storage that remains intact across container stops and restarts.
