Storage and data
Three ways to mount data
| Type | Syntax | Managed by | Use for |
|---|---|---|---|
| Named volume | -v pgdata:/var/lib/postgresql/data or --mount type=volume,src=pgdata,dst=... |
Docker (/var/lib/docker/volumes/) |
Persistent app data, databases |
| Bind mount | -v /host/path:/container/path or --mount type=bind,... |
You (any host path) | Dev hot-reload, config files, host sockets |
| tmpfs | --tmpfs /tmp:size=64m |
Memory | Scratch data, secrets that must not touch disk |
- Volumes survive
docker rm;docker rm -vordocker volume rmdeletes them.docker volume pruneremoves unused ones (dangerous on shared hosts). - An empty named volume mounted over a directory that has content in the image is pre-populated with that content; a bind mount simply hides it.
- Bind mounts expose host files with host UIDs — permission mismatches (container runs as UID 10001, host dir owned by 1000) cause "permission denied".
- Add
:rofor read-only mounts. - Back up a volume by running a throwaway container:
docker run --rm -v pgdata:/data -v $PWD:/backup alpine tar czf /backup/pgdata.tgz -C /data .
The container's writable layer
Data written outside volumes lives in the container layer: lost when the container is removed, slower (copy-on-write), and it bloats /var/lib/docker. Production containers ideally run with a read-only root filesystem (--read-only plus tmpfs for /tmp) — it blocks attackers from writing tools into the container and proves the app has no hidden state.
Databases in containers
Fine for dev and CI. In production it's possible (volumes, backups, careful upgrades), but on AWS a managed service (RDS/Aurora) usually wins: automated backups, Multi-AZ failover, patching. In Kubernetes, stateful databases need StatefulSets, persistent volumes and ideally an operator.
Disk housekeeping
docker system df # space by images, containers, volumes, build cache
docker system prune # stopped containers, unused networks, dangling images, build cache
docker system prune -a --volumes # aggressive: all unused images and volumes
docker builder prune --filter until=72h
/var/lib/docker filling up is one of the most common Docker host incidents. Put it on a separate volume, cap logs, prune on a schedule (carefully), and alert on disk usage.