A Postgres container was removed and the data is gone. How should it have been run?
BeginnerScenarioVolumes, storage and data
Answer
Situation read → Data lived in the container's writable layer (or an anonymous volume that was removed with --rm/-v).
What I'd do →
docker volume create pgdata
docker run -d --name pg -v pgdata:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD_FILE=/run/secrets/pg -p 127.0.0.1:5432:5432 postgres:17
Back up with pg_dump on a schedule; do not treat the volume as a backup.
Why → Named volumes outlive containers and are managed by Docker.
Interview tip → Note that for the postgres image the mount path changed to /var/lib/postgresql for v18+; check the image docs.
More on Volumes, storage and data
Bind mount or named volume for local development of a Node app?BeginnerHow do you back up a named volume?IntermediateTwo containers must share files. What are your options?BeginnerA container needs a writable temp area but the root filesystem is read-only.IntermediateMoving a database volume from one Docker host to another.IntermediateExplain how copy-on-write affects performance of a write-heavy container.Advanced