ποΈ Mastering Docker Storage β Keeping Data Persistent in Containers (Day 19)


πΎ Docker Volumes & Storage β Persisting Data in Containers
π Why Does Docker Storage Matter?
Docker containers are ephemeral, meaning their data disappears once they stop or are removed. But what if you need to store logs, databases, or user uploads permanently?
That's where Docker Volumes & Storage come in! π
In this blog, weβll cover:
β
Why storage is important in Docker
β
Different storage options (Volumes, Bind Mounts, Tmpfs)
β
How to create and manage Docker Volumes
β
Real-world examples of using storage in IT environments
ποΈ Understanding Storage in Docker
Docker provides three main ways to store data:
Storage Type | Description | Use Case |
Volumes π | Managed by Docker, stored outside the containerβs filesystem | Best for databases & app storage |
Bind Mounts π | Directly maps a host directory to a container | Ideal for development/testing |
Tmpfs Mounts β‘ | Stores temporary data in RAM (not on disk) | Best for sensitive, fast-access data |
Key Difference:
Volumes are managed by Docker and are more portable & secure.
Bind Mounts give full control but are tied to host OS paths.
Tmpfs Mounts store data in RAM for speed & security but are lost after restart.
ποΈ Working with Docker Volumes
πΉ 1. Creating a Docker Volume
docker volume create my_data_volume
This command creates a new volume called my_data_volume
for persistent storage.
πΉ 2. Using a Volume in a Container
docker run -d --name my_container -v my_data_volume:/app/data nginx
β
This mounts my_data_volume
inside the container at /app/data
.
β
Even if the container is deleted, the data remains intact in the volume.
πΉ 3. Listing Available Volumes
docker volume ls
π This command displays all Docker volumes on your system.
πΉ 4. Checking Volume Details
docker volume inspect my_data_volume
π Provides details like storage location and mounting details.
πΉ 5. Removing a Volume
docker volume rm my_data_volume
β οΈ Make sure no containers are using the volume before deleting it.
π Using Bind Mounts for Direct File Access
If you need to access host files directly inside a container, use Bind Mounts:
docker run -d --name my_app -v /home/user/data:/app/data nginx
β
This links the /home/user/data
folder on the host to /app/data
inside the container.
β οΈ Changes made inside the container immediately reflect in the host folder.
π₯ Real-World Use Cases in IT
1οΈβ£ Persistent Database Storage (Best for Production Apps)
Problem: Database data should not disappear when a container restarts.
Solution: Use a volume for MySQL/PostgreSQL storage:
docker run -d --name mysql_db -v mysql_data:/var/lib/mysql mysql
β Data remains intact even after container restarts or deletion.
2οΈβ£ Shared Storage for Multi-Container Apps (Best for Microservices)
Problem: Two or more containers need shared access to common files.
Solution: Mount the same volume in multiple containers:
docker run -d --name app1 -v shared_data:/usr/share/data app_image
docker run -d --name app2 -v shared_data:/usr/share/data logging_image
β Both containers share the same persistent data.
3οΈβ£ Temporary Data Processing (Best for Caching & Logs)
Problem: Some data (like logs) should be fast and temporary.
Solution: Use tmpfs
to store logs in RAM instead of disk:
docker run --rm -it --tmpfs /tmp:rw,size=100m ubuntu
β Faster read/write speeds and disappears after container stops.
4οΈβ£ Hosting a Web App with Persistent Storage (Best for Developers)
Scenario: You want to develop a Node.js application and store logs persistently.
docker run -d --name my_node_app -v app_logs:/usr/src/app/logs node
β Logs are stored persistently even if the app restarts.
π― Final Thoughts
Managing storage in Docker is critical for building scalable applications.
π‘ When to Use What?
β
Docker Volumes β Best for databases, logs, and persistent application data.
β
Bind Mounts β Ideal for development environments and direct file access.
β
Tmpfs Mounts β Best for fast, sensitive data (e.g., passwords, temporary logs).
Understanding how to manage storage effectively in Docker will help you build stable, data-persistent, and scalable applications.
π¬ Got any questions? Drop them in the comments below! ππ³
Would you like a hands-on exercise to practice Docker storage? π
Subscribe to my newsletter
Read articles from SRITESH SURANJAN directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

SRITESH SURANJAN
SRITESH SURANJAN
π Passionate DevOps Engineer with expertise in cloud computing, CI/CD, and automation. Skilled in Linux, Docker, Kubernetes, Terraform, Ansible, and Jenkins. I specialize in building scalable, secure, and automated infrastructures, optimizing software delivery pipelines, and integrating DevSecOps practices. Always exploring new ways to enhance deployment workflows and bridge the gap between development and operations.