Filed under · Docker · 2026-07-13 · 5 min read
Docker volumes: why my database data disappeared
A container is not a permanent storage system. I can remove and recreate a container as part of normal Docker usage. That becomes a problem when important database files only exist inside the container's writable layer.
Without persistent storage
Imagine PostgreSQL writes its data inside the database container. I remove the container, then create a fresh one. The new container starts with a fresh filesystem. That can look like the database suddenly "forgot" everything.
Use a named volume
yamlservices:
db:
image: postgres:17
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: password
POSTGRES_DB: app
volumes:
- postgres-data:/var/lib/postgresql/data
volumes:
postgres-data:The named volume exists separately from the lifecycle of an individual container. That means the database container can be replaced while the volume remains available.
Containers and data have different lifecycles
- Container: replaceable runtime
- Volume: persistent data
That separation is useful because rebuilding an application shouldn't automatically mean deleting its database.
Treat containers as replaceable. Give important state its own persistent storage.