☑️Day 25: Docker Volumes and Bind Mounts in Action🚀

☑️Day 25: Docker Volumes and Bind Mounts in Action🚀

🔹Table of Contents :

  • Introduction to Docker Volumes and Bind Mounts

  • Task 1: Persisting MySQL Data with Docker Volumes

  • Task 2: Running a MySQL Container with Named Volumes

  • Task 3: Understanding Bind Mounts and Real-Time Synchronization

  • Key Takeaways


1. Introduction

Docker Volumes are a way to persist data across container lifecycles, while Bind Mounts allow synchronization of files between a host machine and a container. This is extremely useful when working on development projects or databases where you need to ensure data is not lost when containers are stopped or deleted.


✅2. Task 1: Persisting MySQL Data with Docker Volumes

In this task, I learned how to make MySQL data persistent by attaching volumes to the container. This ensures that even after deleting the container, the data will still be available when creating a new one.

Step-by-Step Commands:

  1. Run the MySQL Container
    First, pull the MySQL image and start the container:

     docker container run -itd mysql:latest
    
  2. Create Databases
    Once the MySQL container is running, create two databases:

     docker exec -it <container_id> mysql -uroot -p
     CREATE DATABASE db1;
     CREATE DATABASE db2;
    
  3. Inspect the Container to Locate the Volume
    Use this command to inspect the container and find where the data is stored (usually /var/lib/mysql):

     docker container inspect <container_id>
    
  4. Remove the Existing Container
    Once the databases are created, delete the container:

     docker container rm -f <container_id>
    
  5. Re-create the Container with Attached Volume
    Now, re-create the container with the volume attached to persist the data:

     docker container run -itd -v <volume_id>:/var/lib/mysql mysql:latest
    
  6. Verify Data Persistence
    Access the new container and check if the databases db1 and db2 still exist:

     docker exec -it <new_container_id> mysql -uroot -p
     SHOW DATABASES;
    

✅3. Task 2: Running a MySQL Container with Named Volumes

Named volumes help simplify volume management. Here’s how I used a named volume for a MySQL container.

Step-by-Step Commands:

  1. Run a Container with a Named Volume
    Use the -v option to attach a named volume and -d to run it in detached mode:

     docker container run -d --name sit-mysql -v myvolume:/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql:latest
    
  2. Remove a Specific Volume
    When needed, you can remove a volume by its ID:

     docker volume rm <volume_id>
    
  3. Clean Up Unused Volumes
    To clean up volumes that are not in use:

     docker volume prune
    

✅4. Task 3: Understanding Bind Mounts and Real-Time Synchronization

Bind mounts allow you to synchronize directories between the host machine and the container in real-time. Any changes made inside the container reflect on the host machine and vice versa.

Step-by-Step Commands:

  1. Create a Directory on the Host
    First, create a directory on your host machine and add a file:

     mkdir /bind
     echo "Hello Bind Mount" > /bind/index.html
    
  2. Run a Container with a Bind Mount
    Bind the host directory /bind to /tmp/test inside the container:

     docker container run -d --name sync-container -v /bind:/tmp/test ubuntu:latest
    
  3. Verify the Bind Mount in the Container
    Inside the container, list the files to verify the bind mount:

     docker exec -it sync-container ls -lrth /tmp/test
    
  4. View the Contents Inside the Container
    Check if the file from the host is available inside the container:

     docker exec -it sync-container cat /tmp/test/index.html
    
  5. Create a File Inside the Container
    Now, create a file inside the container. This file will be synchronized with the host machine:

     docker exec -it sync-container echo "Hello from Container" > /tmp/test/test.txt
    
  6. Verify the File on the Host Machine
    On the host machine, check if the file was created in /bind:

     cat /bind/test.txt
    

✅5. Key Takeaways

  • Volumes allow containers to store data independently of the container lifecycle, which is essential for databases like MySQL.

  • Bind Mounts provide real-time synchronization between host and container directories, making development and testing smoother.

  • Both volumes and bind mounts are critical for maintaining data consistency and enabling efficient collaboration between developers.


🚀Thanks for joining me on Day 25! Let’s keep learning and growing together!

Happy Learning! 😊

#90DaysOfDevOps

#Docker #DevOpsJourney #ContinuousLearning #Day25 #DockerVolume #Volume #BindMount #BackupAndRestore #ShubhamLonde

💡
Follow for more updates on LinkedIn , Github and Twitter(X)