πŸ—„οΈ 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 TypeDescriptionUse Case
Volumes πŸ†Managed by Docker, stored outside the container’s filesystemBest for databases & app storage
Bind Mounts πŸ“‚Directly maps a host directory to a containerIdeal 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? 😊

0
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.