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 52/ 160
03. Storage, Volumes & Data Persistence

Backing Up, Restoring & Migrating Docker Volumes

Backup volume data with helper containers and migrate volumes across hosts.

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

In plain words

Prove a recoverable backup artifact, its encryption and its restore command before migrating a host.

Think of it like…

A backup is not a photocopy while the papers are still being shuffled; it is a scheduled, labeled, tested shipment from the drawer to a second building.

Where you’ve seen it

In practice, docker run --rm -v pgdata:/from:ro -v $(pwd)/backups:/to alpine:3.21 tar -czf /to/pgdata.tgz -C /from . is the concrete move for this job: backup volume data with helper containers and migrate volumes across hosts. The release is not accepted until restore the archive into a new test volume, start the database, run a table count/check and retain the test record confirms the result.

Words you’ll hear

Backup
The implementation control in docker run --rm -v pgdata:/from:ro -v $(pwd)/backups:/to alpine:3.21 tar -czf /to/pgdata.tgz -C /from . for backup volume data with helper containers and migrate volumes across hosts.
Restore
The result to observe with restore the archive into a new test volume, start the database, run a table count/check and retain the test record, rather than infer from a successful command alone.
Migration
The production boundary exposed when overwriting /var/lib/postgresql/data with a live tar from another running container, or calling a backup complete before decrypting and restoring it.

Visual diagram

Loading interactive diagram

Theory & deep dive

Backup control path

The concrete mechanism is docker run --rm -v pgdata:/from:ro -v $(pwd)/backups:/to alpine:3.21 tar -czf /to/pgdata.tgz -C /from .. The observable result is restore the archive into a new test volume, start the database, run a table count/check and retain the test record; if those disagree, inspect the runtime evidence before changing unrelated settings.

Backup volume data with helper containers and migrate volumes across hosts. Keep Backup, Restore and Migration in the same deployment record so the intent and the observed state cannot drift apart.

Concrete Backup setup

docker run --rm -v pgdata:/from:ro -v $(pwd)/backups:/to alpine:3.21 tar -czf /to/pgdata.tgz -C /from .

Key takeaway: Prove Restore with restore the archive into a new test volume, start the database, run a table count/check and retain the test record, not with command success alone.

Migration failure boundary

Tar copied while a database writes can capture mixed transactional state. Docker’s volume location is an implementation detail; the helper-container method also gives an auditable way to move named data without copying /var/lib/docker directly. The narrow failure to design for is overwriting /var/lib/postgresql/data with a live tar from another running container, or calling a backup complete before decrypting and restoring it.

Accept this compromise explicitly: Helper snapshots work for generic files; native database tools provide transactional consistency and smaller point-in-time recovery artifacts. The controlled check-restore the archive into a new test volume, start the database, run a table count/check and retain the test record-also identifies the point where a different design is safer.

Deliberate failure test

Exercise this failure safely: overwriting /var/lib/postgresql/data with a live tar from another running container, or calling a backup complete before decrypting and restoring it.

Key takeaway: Helper snapshots work for generic files; native database tools provide transactional consistency and smaller point-in-time recovery artifacts.

How it works, step by step

Step 1 of 4
Design

1. State the boundary

Write down why the workload needs this: Tar copied while a database writes can capture mixed transactional state. Docker’s volume location is an implementation detail; the helper-container method also gives an auditable way to move named data without copying /var/lib/docker directly.

Prove a recoverable backup artifact, its encryption and its restore command before migrating a host.
Key numbers at this step
Intent
Backup

When to use it & trade-offs

What it is

A volume backup can tar files with a short-lived helper mounted over the source volume and a separate host/output mount. A restore must target an empty or deliberately replaced volume, and stateful services should be stopped or use native quiescing/streaming backup for consistency.

Why it matters: Tar copied while a database writes can capture mixed transactional state. Docker’s volume location is an implementation detail; the helper-container method also gives an auditable way to move named data without copying /var/lib/docker directly.

When to reach for it

  • The fit is strongest for this job: backup volume data with helper containers and migrate volumes across hosts. Pair it with restore the archive into a new test volume, start the database, run a table count/check and retain the test record in the runbook.
  • Production Backup changes should make Restore observable through docker run --rm -v pgdata:/from:ro -v $(pwd)/backups:/to alpine:3.21 tar -czf /to/pgdata.tgz -C /from ., rather than trusting the runtime default.
  • Keep a rollback example for the case where overwriting /var/lib/postgresql/data with a live tar from another running container, or calling a backup complete before decrypting and restoring it.

When to avoid or be careful

  • Do not accept command success while the system can still fail by overwriting /var/lib/postgresql/data with a live tar from another running container, or calling a backup complete before decrypting and restoring it.
  • Avoid erasing Migration protection merely to dodge this cost: “Helper snapshots work for generic files; native database tools provide transactional consistency and smaller point-in-time recovery artifacts”.
  • Do not replace the evidence path-restore the archive into a new test volume, start the database, run a table count/check and retain the test record-with a screenshot or a mutable tag.

docker run --rm -v pgdata:/from:ro -v $(pwd)/backups:/to alpine:3.21 tar -czf /to/pgdata.tgz -C /from .: practical choices

Explicit Backup
Best for: Backup volume data with helper containers and migrate volumes across hosts, with a reviewed runbook.

Strengths: A volume backup can tar files with a short-lived helper mounted over the source volume and a separate host/output mount. A restore must target an empty or deliberately replaced volume, and stateful services should be stopped or use native quiescing/streaming backup for consistency.

Watch out: Helper snapshots work for generic files; native database tools provide transactional consistency and smaller point-in-time recovery artifacts.

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

Strengths: It can begin with docker run --rm -v pgdata:/from:ro -v $(pwd)/backups:/to alpine:3.21 tar -czf /to/pgdata.tgz -C /from . and fewer surrounding controls.

Watch out: It silently accepts overwriting /var/lib/postgresql/data with a live tar from another running container, or calling a backup complete before decrypting and restoring it, which makes the result hard to reproduce after promotion.

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

Strengths: Tar copied while a database writes can capture mixed transactional state. Docker’s volume location is an implementation detail; the helper-container method also gives an auditable way to move named data without copying /var/lib/docker directly.

Watch out: The platform adds an ownership and debugging layer; retain restore the archive into a new test volume, start the database, run a table count/check and retain the test record as the direct check.

Interview signals & numbers to know

When this topic comes up

  • docker run --rm -v pgdata:/from:ro -v $(pwd)/backups:/to alpine:3.21 tar -czf /to/pgdata.tgz -C /from .
  • restore the archive into a new test volume, start the database, run a table count/check and retain the test record
  • Failure signature: overwriting /var/lib/postgresql/data with a live tar from another running container, or calling a backup complete before decrypting and restoring it
  • Trade-off: Helper snapshots work for generic files; native database tools provide transactional consistency and smaller point-in-time recovery artifacts.

Benchmarks & rules of thumb

1 quiesced source -
required for raw generic archive consistency
2 separate volumes/paths -
source and backup destination
100% restore test -
the useful measure, not just tar exit status

Common mistakes & follow-ups

Common interview mistakes

  • Treating a successful docker run --rm -v pgdata:/from:ro -v $(pwd)/backups:/to alpine:3.21 tar -czf /to/pgdata.tgz -C /from . as proof and skipping restore the archive into a new test volume, start the database, run a table count/check and retain the test record.
  • Overwriting /var/lib/postgresql/data with a live tar from another running container, or calling a backup complete before decrypting and restoring it
  • Optimizing Backup without accepting that helper snapshots work for generic files; native database tools provide transactional consistency and smaller point-in-time recovery artifacts.

Likely interviewer follow-ups

  • Why does docker run --rm -v pgdata:/from:ro -v $(pwd)/backups:/to alpine:3.21 tar -czf /to/pgdata.tgz -C /from . address Backup here rather than through application code?
  • How would you distinguish healthy Restore from the failure “overwriting /var/lib/postgresql/data with a live tar from another running container, or calling a backup complete before decrypting and restoring it” using restore the archive into a new test volume, start the database, run a table count/check and retain the test record?
  • When would this trade-off-“Helper snapshots work for generic files; native database tools provide transactional consistency and smaller point-in-time recovery artifacts”-justify a different Migration design?
Interview tip: Give one concrete operation-docker run --rm -v pgdata:/from:ro -v $(pwd)/backups:/to alpine:3.21 tar -czf /to/pgdata.tgz -C /from .-and one proof-restore the archive into a new test volume, start the database, run a table count/check and retain the test record. Then name the boundary: overwriting /var/lib/postgresql/data with a live tar from another running container, or calling a backup complete before decrypting and restoring it.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Backing Up, Restoring & Migrating Docker Volumes with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Database Initialization Scripts (/docker-entrypoint-initdb.d)
Next topic
Volume Drivers & Cloud/Network Storage Plugins (NFS, EBS)