Volumes and Data
Persisting data beyond container lifetimes — named volumes, bind mounts, and when to use each
Skills taught: docker.volumes, docker.bind-mounts, docker.data-persistence, docker.volume-commands
Why containers lose data by default
Containers are ephemeral by design. When a container stops and is removed, every file written inside it disappears — the container's writable layer is deleted with it. This is intentional: it makes containers stateless and reproducible. But databases, user uploads, logs, and configuration need to survive. Docker solves this with volumes.
Three ways to mount data
# ── 1. Named Volumes (RECOMMENDED for databases and persistent data) ──
# Docker manages the storage location (usually /var/lib/docker/volumes/)
docker run -d \
-v postgres-data:/var/lib/postgresql/data \
postgres:16
# 'postgres-data' is the named volume; /var/lib/postgresql/data is where
# PostgreSQL stores its data inside the container.
# Volume survives container deletion and is reused when container is recreated.
# ── 2. Bind Mounts (BEST for development) ──
# Mount a specific host directory into the container
docker run -d \
-v $(pwd)/src:/app/src \
-p 3000:3000 \
myapp:dev
# Changes to ./src on your host are immediately visible inside the container.
# Perfect for local dev with hot-reload.
# ── 3. tmpfs Mounts (in-memory, non-persistent) ──
# Stored in host RAM only — perfect for secrets or temp files
docker run --tmpfs /app/tmp:size=100m myapp
Managing named volumes
# Create a volume explicitly
docker volume create postgres-data
# List volumes
docker volume ls
# DRIVER VOLUME NAME
# local postgres-data
# local redis-data
# Inspect a volume (see where Docker stores it on the host)
docker volume inspect postgres-data
# "Mountpoint": "/var/lib/docker/volumes/postgres-data/_data"
# Remove a volume (data is PERMANENTLY deleted)
docker volume rm postgres-data
# Remove all unused volumes
docker volume prune
# Back up a volume to a tar file
docker run --rm \
-v postgres-data:/source \
-v $(pwd):/backup \
alpine tar czf /backup/postgres-backup.tar.gz -C /source .
Choosing the right mount type
- Named volume → database data, user uploads, any data that must persist between container restarts
- Bind mount → local development: mount your source code so changes reflect instantly without rebuilding
- tmpfs → sensitive data that should never touch disk (tokens, temp secrets), or high-speed scratch space
# Mount source code for hot-reload development
docker run -d \
--name dev-server \
-p 3000:3000 \
-v $(pwd):/app \
-v /app/node_modules \
-e NODE_ENV=development \
node:20-alpine \
sh -c "npm install && npm run dev"
# The '-v /app/node_modules' trick:
# - $(pwd):/app mounts your code (overwriting /app in the container)
# - -v /app/node_modules creates an ANONYMOUS volume for node_modules
# This prevents your host (possibly empty) node_modules from
# overwriting the container's installed packages
Warning: Never store database data in the container's writable layer. If you run postgres without a volume and the container is removed (e.g. docker rm -f), all your data is gone permanently. Always use a named volume: -v postgres-data:/var/lib/postgresql/data
Frequently Asked Questions
What is the --mount syntax vs -v?
--mount is the newer, more explicit syntax: --mount type=volume,source=mydata,target=/data/. The -v shorthand is shorter and more common. Docker recommends --mount for clarity in scripts and compose files. Both do the same thing.