How do you persist data beyond a container's life with volumes?
Take a moment.
Say your answer out loud or write it down first — retrieval is what makes it stick. Reveal when you're ready.
Short answer
Mount a volume at the data directory (-v name:/container/path, e.g. -v postgres_data:/var/lib/postgresql); writes there live outside the container and survive stop/rm. Volumes persist until docker volume rm/prune, can be shared across containers, and are inspectable in Desktop's Volumes view.
Detailed explanation
A container's writable layer dies with docker rm — restart a database container and you get an empty database. Volumes are the symlink-like escape hatch: docker volume create log-data then docker run -d -v log-data:/logs … stores everything written to /logs outside the container; Docker auto-creates the volume if missing. The docs' Postgres drill proves it: start docker run --name=db -e POSTGRES_PASSWORD=secret -d -v postgres_data:/var/lib/postgresql postgres:18, docker exec -ti db psql -U postgres to CREATE TABLE + INSERT two rows, docker stop db && docker rm db, then docker run --name=new-db -d -v postgres_data:/var/lib/postgresql postgres:18 and SELECT shows both rows — note POSTGRES_PASSWORD is only needed at bootstrap. Manage growth with docker volume ls, docker volume rm <name> (only when detached), and docker volume prune for all unused; Desktop's Volumes view shows stored data, in-use containers/targets, and export/empty/clone actions. Same volume on multiple containers shares files (log aggregation, pipelines).
Official docs:Docker docs: Persisting container data
Example
docker run --name=db -e POSTGRES_PASSWORD=secret -d -v postgres_data:/var/lib/postgresql postgres:18
docker exec -ti db psql -U postgres -c "CREATE TABLE tasks (id SERIAL PRIMARY KEY, description VARCHAR(100)); INSERT INTO tasks (description) VALUES ('Finish work'), ('Have fun');"
docker stop db && docker rm db # container gone, volume lives on
docker run --name=new-db -d -v postgres_data:/var/lib/postgresql postgres:18
docker exec -ti new-db psql -U postgres -c "SELECT * FROM tasks" # rows still thereInterview tip
Tell the stop/rm/reattach story with the Postgres table surviving — then add that volumes outlive containers and need explicit pruning. That lifecycle point is the interview signal.
Common mistakes
- Expecting data to survive
docker rmwithout a volume. - Running
docker volume rmon an attached volume or forgettingprunelets disk fill silently.