Day 03:Docker Data Persistence: Volumes vs Bind Mounts

"Hello, I'm Kiran Pawar, a passionate Cloud and Devops Engineer with a strong background in cloud automation, configuration, and deployment. My journey in the world of technology has been a thrilling adventure, where I've had the privilege to work with cutting-edge tools and practices.
🚀 As a DevOps Engineer:
I specialize in automating, configuring, and deploying instances in cloud environments and data centers. My expertise extends to DevOps, GitOps, CI/CD pipeline management, HashiCorp Terraform, and containerization. I'm proficient in AWS and Linux/Unix administration, ensuring robust infrastructure and application performance.
🔧 My Tech Stack:
Front-end skills: HTML, CSS, SCSS, Tailwind CSS, Bootstrap, React, Material-UI, JavaScript DevOps toolbox: GIT, OWASP,Nexus,Trivy, Github, Gitlab, Terraform, Ansible, Docker, Kubernetes, Helm, Jenkins, Prometheus, Grafana, Argo CD, AWS EKS.
🌐 My Cloud Expertise:
I have hands-on experience managing AWS services, including EC2, S3, EBS, VPC, ELB, RDS, IAM, Route53, and more.
🔒 Networking and Security:
My skills include managing networking concepts such as TCP/IP protocols, security policies, and subnet interfacing. I have a strong understanding of infrastructure and networking, covering topics like firewalls, IP addressing, DNS, and more.
💡 What Sets Me Apart:
I bring a positive attitude, a strong work ethic, and a collaborative spirit to every project. I'm a self-starter, a fast learner, and an effective team player with strong interpersonal skills. In addition to my DevOps skills, I've developed shell scripts (Bash) for automating tasks and have proficiency in Python scripting. My ability to communicate and manage projects, along with a track record of resolving client issues, adds value to every team I work with. If you're looking for a DevOps engineer who is also well-versed in front-end technologies, feel free to connect with me. Let's explore new possibilities and create exceptional technical solutions together!"
Introduction
In containerized environments, data persistence is crucial to ensure that data is not lost when a container stops or restarts. Docker provides two primary methods for data persistence: volumes and bind mounts. This blog will explain both methods, their differences, and when to use each.
Understanding Docker Storage
When working with containers, the filesystem inside a container is ephemeral by default, meaning any data stored inside the container will be lost when the container is removed. To overcome this limitation, Docker provides two ways to persist data:
Volumes – Managed by Docker and stored in
/var/lib/docker/volumes/on the host.Bind Mounts – Links a directory on the host system to a directory inside the container.
Volumes
What Are Docker Volumes?
Docker volumes are the preferred mechanism for persisting data in containers. They are managed entirely by Docker and offer better performance and flexibility compared to bind mounts.
Creating and Using Docker Volumes
Docker volumes store persistent data independent of container lifecycles. They are the preferred method for managing container data.
List Existing Volumes
Before creating a new volume, check the existing volumes:
docker volume ls
Create a New Volume
To create a dedicated volume:
docker volume create mongodb_volume

Run a MongoDB Container with a Volume
To run MongoDB with persistent storage:
docker run -d --name mongodb -v mongodb_volume:/data/db -p 27017:27017 mongo:latest
Check Running Containers
To verify if the MongoDB container is running:
docker ps

Insert Data into MongoDB
Access the MongoDB shell inside the running container:
docker exec -it mongodb mongosh

Once inside, try the following:
show dbs;
use mydatabase;
db.hello.insertOne({ message: "Hello, Docker!" });
db.hello.find();


Stop and Restart the Container
Stop the MongoDB container:
docker stop mongodb
List stopped containers:
docker ps -a
Restart the MongoDB container:
docker start mongodb

Re-enter the MongoDB shell:
docker exec -it mongodb mongosh
Since we used a Docker volume, all inserted data remains intact after restarting


Advantages of Volumes
Managed by Docker: Volumes are stored in a separate location from the host file system, making them more portable.
Better Performance: Volumes are optimized for containerized workloads.
Easy Backup & Restore: You can easily back up and restore volumes using Docker commands.
Works Well with Swarm: Volumes integrate seamlessly with Docker Swarm for multi-container environments.
Bind Mounts
What Are Bind Mounts?
A bind mount directly links a directory from the host machine to a directory inside the container. Unlike volumes, bind mounts rely on the host’s filesystem structure.
Using Bind Mounts
Bind mounts provide a way to share files between the host machine and containers. They allow real-time synchronization but depend on the host's file structure.
Run a Container Without Network Access
To create an isolated container:
docker run --rm -d --name app1 -v /var/run/docker.sock:/var/run/docker.sock --network none kiran2361993/troubleshootingtools:v1
Run a Container with Docker Socket Mounted
Mounting the Docker socket allows the container to communicate with the Docker daemon:
docker run -d -p 8000:8000 -p 9443:9443 --name portainer \
--restart=always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v portainer_data:/data \
portainer/portainer-ce:2.11.1



Inspect the Running Container
To check details of the running container:
docker inspect troubleshootingtools

Look for the Mounts section to verify the bind mount configuration

Advantages of Bind Mounts
Direct Access to Host Files: Allows seamless access to files from the host system.
Useful for Development: Enables live code reloading when files change on the host.
Simple and Flexible: Can mount any directory on the host machine.
Volumes vs Bind Mounts: Key Differences
| Feature | Volumes | Bind Mounts |
| Managed by Docker | Yes | No |
Stored in /var/lib/docker/volumes/ | Yes | No (Uses host directory) |
| Performance Optimized | Yes | No |
| Security | More Secure | Less Secure (Direct host access) |
| Use Case | Persistent application data | Local development, sharing host files |
Key Takeaways
Volumes persist data across container lifecycles and are managed by Docker.
Bind mounts allow direct access to host files but depend on the host system.
Network isolation ensures containers run securely without external access.
Using these methods, you can efficiently manage containerized applications while ensuring data persistence and security.
