惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
量子位
有赞技术团队
有赞技术团队
博客园 - 三生石上(FineUI控件)
博客园 - Franky
M
MIT News - Artificial intelligence
U
Unit 42
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
J
Java Code Geeks
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
MyScale Blog
MyScale Blog
T
Tailwind CSS Blog
T
The Blog of Author Tim Ferriss
V
V2EX

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Docker Storage & Volumes Internals: Understanding Layers,...
Sreekanth Ku · 2026-05-12 · via DEV Community
Cover image for Docker Storage & Volumes Internals: Understanding Layers, OverlayFS & Persistence

Sreekanth Kuruba

Docker storage often feels like a black box — images appear, containers run, disk space disappears, and volumes behave magically.

This post explains how Docker actually stores data on disk.


1. How Docker Images Are Stored (The Layered Filesystem)

Docker uses union filesystem concepts (implemented via OverlayFS) to build images efficiently.

Each instruction in your Dockerfile creates a new read-only layer.

  • Most RUN, COPY, and ADD instructions create filesystem layers
  • Instructions like ENV, LABEL, CMD, and EXPOSE only add metadata (no filesystem layer)
  • Every layer has a unique SHA256 ID
  • Layers are immutable and shared across images (deduplication)

Key Idea:
Docker images are stacks of immutable layers, not a single filesystem.


2. Storage Driver: overlay2 (Default on Linux)

Modern Docker uses the overlay2 storage driver, based on the Linux kernel’s OverlayFS (a union filesystem implementation).

How OverlayFS Works

OverlayFS combines multiple directories into a single unified view:

  • lowerdir → Read-only image layers
  • upperdir → Writable container layer
  • merged → Unified filesystem view inside the container
  • workdir → Internal working directory required by OverlayFS

Important Behavior

The container never modifies image layers directly.
Instead, it uses copy-on-write — when a file is modified, it is copied into the writable layer first.


3. What Happens When a Container Starts?

When you run a container:

  1. Docker mounts all image layers (read-only)
  2. It creates a thin writable layer on top
  3. The container sees a single unified filesystem (merged view)
  4. All changes go into the writable layer

Container Lifecycle Note

When the container is deleted:

  • Its writable layer is removed
  • Image layers remain unchanged and reusable

However:

  • Data stored in volumes or bind mounts persists independently

4. Volumes vs Bind Mounts

Feature Volumes Bind Mounts
Management Docker-managed User-managed
Location /var/lib/docker/volumes Any host path
Portability High Low
Performance More consistent across platforms Depends on host OS/filesystem
Best Use Case Production, databases Development, live code sync

Recommendation:

  • Use Volumes for production and persistent data
  • Use Bind Mounts for development workflows

5. Why Docker Consumes So Much Disk Space

Docker disk usage grows mainly due to:

  • Unused images and dangling layers
  • Stopped containers with writable layers
  • Orphaned volumes
  • Build cache (including BuildKit cache layers, often the largest hidden consumer)

Useful Commands

# Show overall usage
docker system df

# Detailed breakdown
docker system df -v

# Clean unused data
docker system prune -a --volumes

Enter fullscreen mode Exit fullscreen mode

⚠️ Be careful: prune commands permanently delete unused data.


6. Best Practices for Storage

  • Use volumes for persistent and stateful data
  • Avoid storing important data inside container writable layers
  • Use multi-stage builds to reduce image size
  • Clean unused resources regularly
  • Monitor disk usage with docker system df
  • Minimize unnecessary files inside images (logs, caches, temp files)

Summary

Docker storage is built on three core principles:

  • Immutable image layers (shared and reusable)
  • Copy-on-write writable container layer
  • External persistence via volumes and bind mounts

Understanding this model helps you:

  • Reduce disk usage
  • Debug storage issues faster
  • Design efficient container architectures
  • Avoid accidental data loss

Next Topic

Docker Security Internals: Namespaces, cgroups, Capabilities, Rootless Docker & Best Practices