Skip to content
tezvyn:
Menu

Persist PostgreSQL data across compose down

Source: interviewEasyHow cards are made

Summary

volume persistence basics.

Key points

define a named volume and mount it at the database's data directory (/var/lib/postgresql/data); named volumes survive compose down.

WHAT THIS TESTS It checks whether you understand that a container's writable layer is ephemeral and how named volumes decouple data lifetime from container lifetime.

A GOOD ANSWER COVERS Anything written only to the container's writable layer disappears when the container is removed, which is what compose down does. To persist Postgres data you declare a named volume and mount it where Postgres stores data. In the YAML you add a top-level volumes: key with an entry like pgdata:, and under the db service you add volumes: with pgdata:/var/lib/postgresql/data. Now the data lives in a Docker-managed volume that survives container removal; docker compose down stops and removes containers but leaves named volumes, so the next docker compose up reattaches the same volume with your data intact. The important caveat: docker compose down -v explicitly deletes named volumes, so avoid -v unless you truly want a clean slate.

COMMON WRONG ANSWERS Relying on the writable layer and being surprised data vanishes. Mounting the wrong path, not the Postgres data directory. Confusing bind mounts with named volumes. Routinely using down -v and wondering why data is gone.

LIKELY FOLLOW-UPS What does down -v do? How does a named volume differ from a bind mount here? Where does Docker actually store the volume on disk, and how do you back it up?

ONE CONCRETE EXAMPLE The db service maps pgdata:/var/lib/postgresql/data and the file declares volumes: pgdata:. You insert rows, run docker compose down, then docker compose up -d; the rows are still there because the pgdata named volume was never removed. Only docker compose down -v would have wiped them.

Interview question

You mount a named volume at the Postgres data directory, then run docker compose down. Why does the data survive?

  • a.Named volumes are managed independently of containers, so removing containers leaves them intactCorrect
  • b.down never removes anything, only stops services
  • c.Postgres replicates data to the host's RAM
  • d.down recreates the volume from a backup snapshot
Why?

Named volumes have a lifetime separate from containers, so down (which removes containers) leaves the volume and its data; there is no snapshot, and down does remove containers unless you add -v which would also delete the volume.

Just read this? Test yourself on what you have been reading.

Read the original → docs.docker.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles