Goal
Recreate db (postgres:17, password secret) with its data in a volume called pgdata, mounted at /var/lib/postgresql/data.
Everything a container writes goes into its own writable layer — and docker rm throws that layer away. For a database, that means docker rm is "delete all data".
A volume is storage Docker manages separately from any container. -v pgdata:/var/lib/postgresql/data mounts the volume pgdata at the path where Postgres keeps its files. Remove the container and the volume stays; mount it into a new container and the data is all still there.
Try it
Replace the database container with one whose data lives in a volume. Watch the disk appear and plug in.