Lesson 5 of 30
A container's own writable layer dies with the container. Anything that needs to outlive it — a database's data, an upload, a local source tree for hot reload — has to live somewhere else entirely.
12 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 3
A production database container is started without any volume declared for its data directory. After a redeploy replaces the container, what happens to the data?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
A container's writable layer is deliberately ephemeral — that's what makes "delete it and start a fresh one" a safe, normal operation rather than a risky one. But some data genuinely needs to survive a container's death: a database's actual rows, user-uploaded files, build caches worth keeping warm. Docker has three distinct ways to attach external storage to a container, and they solve different problems:
docker volume create, or declared in Compose as seen in the previous lesson). The normal choice for anything a container needs to persist: a database's data directory, for example.# Named volume: Docker manages where this actually lives
docker run -v db-data:/var/lib/postgresql/data postgres:16
# Bind mount: an exact host path, for local dev hot-reload
docker run -v $(pwd)/src:/app/src myapp:dev
# tmpfs: memory only, gone when the container stops
docker run --tmpfs /app/cache myapp:dev
The -v flag's left-hand side is what distinguishes them: a name (db-data) means a Docker-managed named volume; a path starting with / or . means a bind mount to that exact host location. Same flag, genuinely different mechanism underneath.
docker volume prune deletes every volume not currently attached to a running container — including ones you meant to keep for a container that's simply stopped, not removed. This is a real, recurring way to accidentally lose local database data.docker compose down, but not docker compose down -v (from the previous lesson) or an explicit docker volume rm — persistence is real, but not permanent against every cleanup command."Why did our staging database lose all its data after a redeploy?" is a real incident-postmortem-style question, and the strong answer traces it to a specific, nameable cause — no volume was ever declared for the database's data directory, so a fresh container started with a fresh, empty writable layer — rather than a vague "something went wrong with Docker."