Running Stateful Databases in Docker (PostgreSQL, MySQL, Redis, MongoDB)
Run popular databases in Docker with durable volumes and sensible production caveats.
In plain words
Containerize the process only after the storage and recovery contract is at least as strong as it would be on a native host.
Think of it like…
The database container is the engine block; the volume is the engine’s crankcase, and backups are the spare vehicle stored at another site.
Where you’ve seen it
In practice, docker run -d --name pg --memory=2g -e POSTGRES_PASSWORD_FILE=/run/secrets/pg -v pgdata:/var/lib/postgresql/data postgres:17 is the concrete move for this job: run popular databases in Docker with durable volumes and sensible production caveats. The release is not accepted until run pg_isready, stop -t 60 pg, inspect the clean shutdown log and perform pg_basebackup or pg_dump into separate storage confirms the result.
Words you’ll hear
- Postgres
- The implementation control in
docker run -d --name pg --memory=2g -e POSTGRES_PASSWORD_FILE=/run/secrets/pg -v pgdata:/var/lib/postgresql/data postgres:17for run popular databases in Docker with durable volumes and sensible production caveats. - Redis
- The result to observe with
run pg_isready, stop -t 60 pg, inspect the clean shutdown log and perform pg_basebackup or pg_dump into separate storage, rather than infer from a successful command alone. - Durable Volumes
- The production boundary exposed when mounting pgdata from different PostgreSQL major versions, or calling docker cp of a live random data directory a consistent backup.
Visual diagram
Theory & deep dive
Postgres control path
The concrete mechanism is docker run -d --name pg --memory=2g -e POSTGRES_PASSWORD_FILE=/run/secrets/pg -v pgdata:/var/lib/postgresql/data postgres:17. The observable result is run pg_isready, stop -t 60 pg, inspect the clean shutdown log and perform pg_basebackup or pg_dump into separate storage; if those disagree, inspect the runtime evidence before changing unrelated settings.
Run popular databases in Docker with durable volumes and sensible production caveats. Keep Postgres, Redis and Durable Volumes in the same deployment record so the intent and the observed state cannot drift apart.
docker run -d --name pg --memory=2g -e POSTGRES_PASSWORD_FILE=/run/secrets/pg -v pgdata:/var/lib/postgresql/data postgres:17
run pg_isready, stop -t 60 pg, inspect the clean shutdown log and perform pg_basebackup or pg_dump into separate storage, not with command success alone.Durable Volumes failure boundary
A volume protects files from container deletion but not bit rot, accidental DROP, volume loss or incompatible image upgrades. Redis AOF/RDB and database write-ahead logs also expose why “copy the directory while it is live” is not a reliable backup. The narrow failure to design for is mounting pgdata from different PostgreSQL major versions, or calling docker cp of a live random data directory a consistent backup.
Accept this compromise explicitly: Single-node containers are operationally cheap but leave failover and volume repair to the operator; managed databases package those guarantees at higher cost. The controlled check-run pg_isready, stop -t 60 pg, inspect the clean shutdown log and perform pg_basebackup or pg_dump into separate storage-also identifies the point where a different design is safer.
Exercise this failure safely: mounting pgdata from different PostgreSQL major versions, or calling docker cp of a live random data directory a consistent backup.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: A volume protects files from container deletion but not bit rot, accidental DROP, volume loss or incompatible image upgrades. Redis AOF/RDB and database write-ahead logs also expose why “copy the directory while it is live” is not a reliable backup.
- Intent
- Postgres
When to use it & trade-offs
What it is
A database container is replaceable when its data directory is a durable volume, but its engine still needs exact UID ownership, fsync support, resource limits, backups and a compatible shutdown path. PostgreSQL, MySQL, Redis and MongoDB differ in durability and replication defaults.
Why it matters: A volume protects files from container deletion but not bit rot, accidental DROP, volume loss or incompatible image upgrades. Redis AOF/RDB and database write-ahead logs also expose why “copy the directory while it is live” is not a reliable backup.
When to reach for it
- The fit is strongest for this job: run popular databases in Docker with durable volumes and sensible production caveats. Pair it with
run pg_isready, stop -t 60 pg, inspect the clean shutdown log and perform pg_basebackup or pg_dump into separate storagein the runbook. - Production Postgres changes should make Redis observable through
docker run -d --name pg --memory=2g -e POSTGRES_PASSWORD_FILE=/run/secrets/pg -v pgdata:/var/lib/postgresql/data postgres:17, rather than trusting the runtime default. - Keep a rollback example for the case where mounting pgdata from different PostgreSQL major versions, or calling docker cp of a live random data directory a consistent backup.
When to avoid or be careful
- Do not accept command success while the system can still fail by mounting pgdata from different PostgreSQL major versions, or calling docker cp of a live random data directory a consistent backup.
- Avoid erasing Durable Volumes protection merely to dodge this cost: “Single-node containers are operationally cheap but leave failover and volume repair to the operator; managed databases package those guarantees at higher cost”.
- Do not replace the evidence path-
run pg_isready, stop -t 60 pg, inspect the clean shutdown log and perform pg_basebackup or pg_dump into separate storage-with a screenshot or a mutable tag.
docker run -d --name pg --memory=2g -e POSTGRES_PASSWORD_FILE=/run/secrets/pg -v pgdata:/var/lib/postgresql/data postgres:17: practical choices
Strengths: A database container is replaceable when its data directory is a durable volume, but its engine still needs exact UID ownership, fsync support, resource limits, backups and a compatible shutdown path. PostgreSQL, MySQL, Redis and MongoDB differ in durability and replication defaults.
Watch out: Single-node containers are operationally cheap but leave failover and volume repair to the operator; managed databases package those guarantees at higher cost.
Strengths: It can begin with docker run -d --name pg --memory=2g -e POSTGRES_PASSWORD_FILE=/run/secrets/pg -v pgdata:/var/lib/postgresql/data postgres:17 and fewer surrounding controls.
Watch out: It silently accepts mounting pgdata from different PostgreSQL major versions, or calling docker cp of a live random data directory a consistent backup, which makes the result hard to reproduce after promotion.
Strengths: A volume protects files from container deletion but not bit rot, accidental DROP, volume loss or incompatible image upgrades. Redis AOF/RDB and database write-ahead logs also expose why “copy the directory while it is live” is not a reliable backup.
Watch out: The platform adds an ownership and debugging layer; retain run pg_isready, stop -t 60 pg, inspect the clean shutdown log and perform pg_basebackup or pg_dump into separate storage as the direct check.
Interview signals & numbers to know
When this topic comes up
- docker run -d --name pg --memory=2g -e POSTGRES_PASSWORD_FILE=/run/secrets/pg -v pgdata:/var/lib/postgresql/data postgres:17
- run pg_isready, stop -t 60 pg, inspect the clean shutdown log and perform pg_basebackup or pg_dump into separate storage
- Failure signature: mounting pgdata from different PostgreSQL major versions, or calling docker cp of a live random data directory a consistent backup
- Trade-off: Single-node containers are operationally cheap but leave failover and volume repair to the operator; managed databases package those guarantees at higher cost.
Benchmarks & rules of thumb
- 17 major version
- must be planned explicitly for PostgreSQL data upgrades
- 60 seconds
- example long graceful database stop
- 2 backup classes
- logical export and physical/WAL-based recovery are different
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
docker run -d --name pg --memory=2g -e POSTGRES_PASSWORD_FILE=/run/secrets/pg -v pgdata:/var/lib/postgresql/data postgres:17as proof and skippingrun pg_isready, stop -t 60 pg, inspect the clean shutdown log and perform pg_basebackup or pg_dump into separate storage. - Mounting pgdata from different PostgreSQL major versions, or calling docker cp of a live random data directory a consistent backup
- Optimizing Postgres without accepting that single-node containers are operationally cheap but leave failover and volume repair to the operator; managed databases package those guarantees at higher cost.
Likely interviewer follow-ups
- Why does
docker run -d --name pg --memory=2g -e POSTGRES_PASSWORD_FILE=/run/secrets/pg -v pgdata:/var/lib/postgresql/data postgres:17address Postgres here rather than through application code? - How would you distinguish healthy Redis from the failure “mounting pgdata from different PostgreSQL major versions, or calling docker cp of a live random data directory a consistent backup” using
run pg_isready, stop -t 60 pg, inspect the clean shutdown log and perform pg_basebackup or pg_dump into separate storage? - When would this trade-off-“Single-node containers are operationally cheap but leave failover and volume repair to the operator; managed databases package those guarantees at higher cost”-justify a different Durable Volumes design?
docker run -d --name pg --memory=2g -e POSTGRES_PASSWORD_FILE=/run/secrets/pg -v pgdata:/var/lib/postgresql/data postgres:17-and one proof-run pg_isready, stop -t 60 pg, inspect the clean shutdown log and perform pg_basebackup or pg_dump into separate storage. Then name the boundary: mounting pgdata from different PostgreSQL major versions, or calling docker cp of a live random data directory a consistent backup.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Running Stateful Databases in Docker (PostgreSQL, MySQL, Redis, MongoDB) with generative AI interview Q&A and hands-on practice.