~/signup/compose.yaml runs web and db on the default network, and Postgres keeps its data in an anonymous volume. Every docker compose down loses the signups.
See the current state first:
sh
cat compose.yaml
docker compose up -d
docker network ls
docker volume ls
docker compose downOpen the file:
Make these changes:
| Where | Add |
|---|---|
web and db | networks: with the item - backend |
db | volumes: with the item - db-data:/var/lib/postgresql/data |
| top level | networks: with key backend: |
| top level | volumes: with key db-data: |
Save, check the file and start the stack:
sh
docker compose config
docker compose up -d
docker network ls
docker volume lsAdd a signup, then remove and recreate the containers:
sh
curl -s -X POST 'localhost:8000/signup?name=ada'
docker compose down
docker compose up -d
curl -s localhost:8000The count should still be 1. docker compose logs db shows whether Postgres initialised a new database or reused the volume.