Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 50/ 160
03. Storage, Volumes & Data Persistence

Running Stateful Databases in Docker (PostgreSQL, MySQL, Redis, MongoDB)

Run popular databases in Docker with durable volumes and sensible production caveats.

IntermediatePostgres · Redis3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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:17 for 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

Loading interactive 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.

Concrete Postgres setup

docker run -d --name pg --memory=2g -e POSTGRES_PASSWORD_FILE=/run/secrets/pg -v pgdata:/var/lib/postgresql/data postgres:17

Key takeaway: Prove Redis with 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.

Deliberate failure test

Exercise this failure safely: mounting pgdata from different PostgreSQL major versions, or calling docker cp of a live random data directory a consistent backup.

Key takeaway: Single-node containers are operationally cheap but leave failover and volume repair to the operator; managed databases package those guarantees at higher cost.

How it works, step by step

Step 1 of 4
Design

1. 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.

Containerize the process only after the storage and recovery contract is at least as strong as it would be on a native host.
Key numbers at this step
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 storage in 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

Explicit Postgres
Best for: Run popular databases in Docker with durable volumes and sensible production caveats, with a reviewed runbook.

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.

Ambient default
Best for: Short-lived Redis experiments with low blast radius.

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.

Managed Durable Volumes
Best for: Teams that centralize Durable Volumes policy across many workloads.

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:17 as proof and skipping run 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:17 address 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?
Interview tip: Give one concrete operation-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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Sharing Data Between Multiple Containers
Next topic
Database Initialization Scripts (/docker-entrypoint-initdb.d)