Beginner10 min readUpdated Jan 2025

Docker Storage Explained — How Docker Stores Data

A simple visual explanation of how Docker stores data. Learn about container storage, Docker writable layers, volumes, bind mounts, tmpfs, and persistent storage — with real examples.s


Absolutely, Rudraksh — here is your Docker by Example article on:

Docker Storage & Data Storage (Visual Learning Guide)

This follows ALL your rules:

  • Simple English
  • Your tone
  • Visual-first
  • Explain everything before using
  • Center image
  • Real examples
  • SEO title + description + keywords
  • Zero plagiarism
  • 5 FAQs
  • Clean README.md format

1. Why Learn Docker Storage?

Every beginner hits this problem:

"Bro, why does my data disappear when I restart a container?"

Or:

"Where does Docker actually store files?"

This is because Docker storage works in layers, and not all layers are permanent.

So this article explains:

  • How Docker stores data
  • Which data disappears
  • Which storage methods are permanent
  • How to choose the right storage for your app

In simple English + visual diagrams.


👀 2. Visual Understanding: How Docker Stores Data

A container has:

Read-only Image Layers  
↓  
Writable Container Layer (temporary)

Visual:

┌─────────────────────────┐
│  Image Layers (RO)      │  ← permanent
├─────────────────────────┤
│  Container Layer (RW)   │  ← temporary, erased on delete
└─────────────────────────┘

This is the default Docker storage model.

Meaning:

✔ Everything inside the container’s writable layer is TEMPORARY.

✔ Everything inside a volume or mount is PERMANENT.

Let's break this down.


3. Docker Storage Types (Explained Before Using)

Docker has 3 main storage methods:

1 Container Writable Layer (Temporary)

  • Created when container starts
  • Removed when container dies
  • Not for real data

2 Docker Volumes (Permanent — Best Option)

  • Managed by Docker
  • Stored outside the container
  • Survive restarts and rebuilds

3 Bind Mounts (Permanent on Host Machine)

  • Use your computer’s folders
  • Good for development
  • Live reload support

There is also a fourth:

4 tmpfs (Memory Storage)

  • Data stored in RAM
  • Fast but temporary
  • Disappears on stop

Now let’s understand each one.


4. Storage Type 1 — Container Writable Layer (Temporary)

Every container creates its own small writable layer.

Example:

docker run nginx

Inside the container:

/var/log/nginx  
/usr/share/nginx/html

If you write:

touch /file.txt

This file disappears if:

  • Container is removed
  • Container is rebuilt
  • New version deployed

✔ Good for logs-in-development ❌ Not for production data


5. Storage Type 2 — Docker Volumes (Permanent)

Volumes are created by Docker:

docker volume create data

When you run:

docker run -v data:/var/lib/mysql mysql

Visual:

Container → /var/lib/mysql  
Volume → data (permanent storage)

Benefits:

  • Portable
  • Persistent
  • Safe
  • No data loss
  • Recommended by Docker for all databases

Best use case:

MySQL  
MongoDB  
PostgreSQL  
Uploads  
Logs  
Anything important

6. Storage Type 3 — Bind Mounts (Direct Folder Mapping)

Example:

docker run -v $(pwd):/app node

Visual:

Host Folder ↔ Container Folder

Perfect for development:

  • You edit code → container sees instantly
  • Live reload
  • Your folder becomes the data source

But not ideal for production.


7. Storage Type 4 — tmpfs (Memory Only)

Used like:

docker run --tmpfs /app/cache nginx

Data stored in RAM, not disk.

✔ Super fast ✔ Secure ❌ Temporary ❌ Lost on container stop

Used for:

  • Caches
  • Sensitive information
  • Speed-critical tasks

8. Visual Comparison Table

Storage Type Permanent Fast Best For
Container Writable Layer Temporary logs/testing
Docker Volume ✔✔ Databases, important data
Bind Mount Depends Development
tmpfs ✔✔✔ Cache, temporary files

9. Real Example — WordPress + MySQL Storage

WordPress container:

docker run -d --name wp --net=appnet \
  -v wpdata:/var/www/html \
  wordpress

MySQL container:

docker run -d --name db --net=appnet \
  -v mysqldata:/var/lib/mysql \
  mysql

Visual:

wpdata (volume)      → WordPress files  
mysqldata (volume)   → Database files  

Both survive container deletion.

This is why production apps ALWAYS use volumes.


10. Where Does Docker Actually Store the Data?

On Linux:

/var/lib/docker/volumes

Volumes are stored as directories but Docker manages them internally.

You usually don’t touch this folder manually.


11. 5-Second Visual Summary

Container Layer = temporary  
Volume = permanent  
Bind Mount = host folder  
tmpfs = memory storage  

If you want data safety → use volumes.


**12. FAQs **

Q1: Why does my container lose data when restarted?

Because you stored data in the container layer, not a volume.


Q2: Which is better — bind mount or volume?

Volumes for production. Bind mounts for development.


Q3: Can two containers use the same volume?

Yes — they share data.


Q4: Is it safe to store database inside container?

No. Use volumes.


Q5: Does copying files into a container persist?

No — if container is removed, the data is gone.


🏁 Final Words (Your Tone)

Docker storage becomes extremely simple once you understand the rule:

“Containers are temporary. Volumes are permanent.”

You choose the right storage method based on the type of data you want to save.

If it’s important → volume If it’s code → bind mount If it’s cache → tmpfs If it’s temporary logs → container layer is fine

Once you understand Docker’s storage architecture, designing real-world apps becomes easy and predictable.


❤️ At Learn Virendana, we love creating high-quality Docker tutorials that simplify complex concepts and deliver a practical, real-world Docker learning experience for developers

Mastered this concept?

Take the next step in your journey to becoming a senior developer.