Docker Volumes deep concept

Amitabh soniAmitabh soni
3 min read

🚀 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:
    mkdir mysql-data && cd mysql-data
    pwd
  1. Running a MySQL Docker Container with Volume Mounting

    • Pulled the MySQL image and ran it while mounting the mysql-data directory:
    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.

  1. Verifying Container Creation

    • Checked running containers with:
    docker ps
  1. Accessing MySQL in the Container

    • Accessed the container’s shell and launched MySQL in interactive mode:
    docker exec -it <container_id> bash
    mysql -u root -p

  1. Creating a Database and Table in MySQL

    • Checked initial databases, created a new database mydb, and added a table names:
    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;

  1. Confirming Volume Data

    • After stopping the container, navigated to mysql-data and confirmed the database files:
    cd mysql-data

  1. Restarting Container

    • Restarted and verified the data:

  2. Deleting Volume

    • Removed the volume with elevated permissions:
    sudo rm -rf mysql-data


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:
    docker volume ls
    docker volume create mysql-data
    docker volume inspect mysql-data

  1. Running MySQL with the New Volume

    • Mounted mysql-data volume for MySQL data persistence:
    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

  1. Creating Database and Table for Volume Test

    • Created rank_list database, rankers_table, and inserted data for testing:
    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;

  1. Verifying Volume Data

    • Located rank_list in the specified volume path:
    sudo su
    cd /var/lib/docker/volumes/mysql-data/_data
    ls

  1. Restarting and Re-verifying Data

    • Stopped, removed, and re-ran the container to ensure data persistence.


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.

0
Subscribe to my newsletter

Read articles from Amitabh soni directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Amitabh soni
Amitabh soni

DevOps Enthusiast | Passionate Learner in Tech | BSc IT Student I’m a second-year BSc IT student with a deep love for technology and an ambitious goal: to become a DevOps expert. Currently diving into the world of automation, cloud services, and version control, I’m excited to learn and grow in this dynamic field. As I expand my knowledge, I’m eager to connect with like-minded professionals and explore opportunities to apply what I’m learning in real-world projects. Let’s connect and see how we can innovate together!