How Docker volume lifecycles work, why --volumes is dangerous, and how to protect persistent data.
You remove and rebuild your Compose app:
docker compose down docker compose up -d --build
Enter fullscreen mode
Exit fullscreen mode
The containers are brand new, the image is rebuilt, and yet your database, accounts, and settings are all still there.
Is Docker failing to clean up? No. This is exactly how persistent storage is designed to work.
1. Containers and volumes have different lifecycles
A container is disposable. A volume is built to outlive it.
services : app : image : alpine:3.20 command : [ " sleep" , " infinity" ] volumes : - app-data:/data
volumes : app-data :
Enter fullscreen mode
Exit fullscreen mode
Here app-data is a named volume . Compose creates it and mounts it at /data . When the container is removed, the volume stays on the Docker host.
This is what makes databases, uploads, and configuration survive upgrades.
2.…