Skip to main content

Volumes, Bind Mounts, and Data Lifecycle

Container filesystems are ephemeral -- when you remove a container, everything inside it is deleted. If you are running a database, uploading files, or storing any data that matters, you must mount external storage. This module covers how.

Golden Rule

If you run a database without a volume, your data will be deleted when the container is removed.

How Storage Works in Docker

flowchart TD
subgraph Host["Docker Host"]
subgraph DockerArea["/var/lib/docker/volumes/"]
Vol["Named Volume<br/>(managed by Docker)"]
end
subgraph UserArea["Any host path"]
Bind["Bind Mount<br/>(your files)"]
end
RAM["RAM / tmpfs"]
end

subgraph Container
D1["/var/lib/postgresql/data"]
D2["/app/src"]
D3["/tmp/cache"]
end

Vol -->|"Survives container deletion"| D1
Bind -->|"Live sync with host"| D2
RAM -->|"Lost on stop"| D3

style Vol fill:#e8f5e9,stroke:#2e7d32
style Bind fill:#e3f2fd,stroke:#1565c0
style RAM fill:#fff3e0,stroke:#ef6c00

What You Will Learn

LessonWhat It Covers
Volumes vs Bind MountsWhen to use each storage type, with practical examples
Backup and RestorePortable patterns for backing up and migrating volume data

Quick Decision Guide

QuestionAnswer
Is it a database or persistent app state?Use a named volume
Is it source code during development?Use a bind mount
Is it temporary data (cache, sessions)?Use tmpfs
Is it a config file injected from the host?Use a bind mount (read-only)

Prerequisites

  • Understand containers and docker run (Module 4)
  • Understand Docker networking basics (Module 5)