Day 5: Conquering Docker Volumes with Grok’s Help—Data That Sticks Around!


Day 5 was all about Docker volumes—those magical storage spaces that keep your data safe even when containers vanish. I dove hands-on into creating volumes, persisted MySQL data, played with Nginx, and peeked into 2025 trends on X. Let’s unpack this data-saving adventure!
Volumes: The Superheroes of Data Persistence
I started by creating a volume named app-data with a simple command:
bash
docker volume create app-data
Think of app-data as a trusty backpack—separate from the container, it holds data like a champ. I verified it with docker volume ls and peeked at its details using docker volume inspect app-data. Next app-data’s mountpoint is on my host at /var/lib/docker/volumes/app-data/_data, but I don’t need to mess with it directly—Docker handles it.
Next, I ran a MySQL container, mounting app-data to /var/lib/mysql:
bash
docker run -d --name mysql-db -v app-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=rootpass mysql:8
I created a database called myapp inside it:
bash
docker exec -it mysql-db mysql -uroot -prootpass
CREATE DATABASE myapp;
EXIT;
Then, I stopped and removed the container:
bash
docker stop mysql-db
docker rm mysql-db
But wait! The data isn’t gone. I recreated the container with the same volume:
bash
docker run -d --name mysql-db -v app-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=rootpass mysql:8
And checked:
bash
docker exec -it mysql-db mysql -uroot -prootpass -e "SHOW DATABASES;"
Boom! myapp was still there. Volumes FTW! I also tested a temp volume, temp-data, with a busybox container, wrote a file, and verified it persisted until I removed the volume with docker volume rm temp-data.
Volume Commands and Grok Insights
I got cozy with volume commands:
docker volume ls: Lists all volumes.
docker volume inspect app-data: Shows details like mountpoint.
docker volume rm temp-data: Deletes a volume.
docker volume prune: Cleans up unused volumes.
Grok clarified the difference between volumes and bind mounts in 100 words: volumes are Docker-managed, portable, and ideal for production, while bind mounts map host paths, suiting development. I also asked, “Explain how Docker volumes are used in Kubernetes in 150 words, with a 2025 example.” Grok explained that in Kubernetes, volumes map to PersistentVolumes (PVs) and PersistentVolumeClaims (PVCs). A 2025 AI healthcare app might use a PVC to access a PV backed by a Docker volume, storing patient data across pod restarts, ensuring compliance and scalability.
Using DeepSearch, I found X posts on Docker volumes in 2025. A post by @baeldung (March 30, 2025) linked to a blog on backing up containers with volumes, with 4,270 views—useful for production! I summarized it in 50 words: “Backup strategies for Docker volumes ensure data integrity, using tools like docker cp or volume plugins for cloud storage, critical for 2025’s data-driven apps.”
Mini-Project: Nginx with a Volume
For fun, I created web-data and ran an Nginx container:
bash
docker volume create web-data
docker run -d --name web-app -v web-data:/usr/share/nginx/html -p 8084:80 nginx
I updated the default page:
bash
docker exec web-app sh -c "echo '<h1>Day 5 Rocks!</h1>' > /usr/share/nginx/html/index.html"
Checked http://localhost:8084—it said, “Day 5 Rocks!” I stopped and removed web-app, but the volume kept the custom page. If I recreate it, the page persists. Volumes are like data glue!
Subscribe to my newsletter
Read articles from Usman Jap directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
