Interview Prep/Docker/Volumes, storage and data

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.

All Docker questions Roadmap: Docker & containers

More on Volumes, storage and data