containerization-and-devops

Class 5 - Docker Volumes and Bind Mounts (Hands-on)

Objective


Environment Used


Experiment Execution with Screenshots

🔹 Step 1: Check Existing Docker Volumes

Initially verifies if any volumes exist.

docker volume ls

Step 1 -- Docker Volume List


🔹 Step 2: Create a Named Volume

Creates a volume named testvol1 and verifies it.

docker volume create testvol1
docker volume ls

Step 2 -- Create Volume


🔹 Step 3: Mount Volume to Container and Create File

Mounts the volume to /home/app and writes data.

docker run -it --rm -v testvol1:/home/app ubuntu /bin/bash
echo "sapid=500121882" >> /home/app/sapid.txt
cat /home/app/sapid.txt
exit

Step 3 -- Write Data to Volume


🔹 Step 4: Verify Data Persistence

Runs a new container and checks if file persists.

docker run -it --rm -v testvol1:/home/app ubuntu /bin/bash
cat /home/app/sapid.txt

Step 4 -- Volume Persistence


🔹 Step 5: Bind Mount with Host Directory

Creates a host folder and mounts it to container.

docker run -it --rm -v $(pwd)/hostfolder:/data ubuntu /bin/bash
echo "Hello from bind mount" >> /data/test.txt
cat /data/test.txt
exit

Step 5 -- Bind Mount Write


🔹 Step 6: Verify Data on Host System

Checks file directly from host directory.

cat hostfolder/test.txt

Step 6 -- Verify Host File


🔹 Step 7: Create Another Named Volume

Creates volume myvolume.

docker volume create myvolume
docker volume ls

Step 7 -- Create myvolume


🔹 Step 8: Write Data into Named Volume

Mounts myvolume and writes data.

docker run -it --rm -v myvolume:/data ubuntu /bin/bash
echo "Hello from named volume" >> /data/file.txt
cat /data/file.txt
exit

Step 8 -- Write Named Volume


🔹 Step 9: Verify Named Volume Persistence

Runs new container to confirm data persistence.

docker run -it --rm -v myvolume:/data ubuntu /bin/bash
cat /data/file.txt

Step 9 -- Verify Named Volume


Result

Docker volumes and bind mounts were successfully created and tested.
Data persisted across multiple container executions.


Learning Outcome


- Previous Class | Theory Index | Next Class ->