Skip to content

20. Backup & Disaster Recovery

Because containers are ephemeral, backing up the stateless application container itself is unnecessary (you just pull the image again). However, backing up the persistent state—your databases and user-uploaded files residing in Docker Volumes—is absolutely critical to surviving a catastrophic server failure.

You should rarely back up a database by simply copying its raw physical files off the hard drive, as this can lead to corrupted backups if the database is actively writing data. Instead, use the database’s native dump utilities.

You can use docker exec to trigger the pg_dump utility inside the running database container, securely piping the output to a file on your host machine.

Terminal window
# Dump the entire database to a file named backup.sql on the host
docker exec -t my-postgres pg_dump -U myuser -d mydb > backup.sql
# Compress it to save massive amounts of space
docker exec -t my-postgres pg_dump -U myuser -d mydb | gzip > backup.sql.gz

Restoring PostgreSQL: To restore the data into a fresh container, pipe the SQL file back in.

Terminal window
cat backup.sql | docker exec -i my-postgres psql -U myuser -d mydb

Similarly, MySQL uses the mysqldump utility.

Terminal window
docker exec -t my-mysql mysqldump -u root -p'my_password' mydb > backup.sql

Restoring MySQL:

Terminal window
cat backup.sql | docker exec -i my-mysql mysql -u root -p'my_password' mydb

For backing up raw files (like a directory of user-uploaded images stored in a named volume), you must back up the Docker Volume directly.

Running Container (Detached)
↓
Shared Named Volume (Data resides here)
↓
Temporary Backup Container (Mounts the volume)
↓
Tar/Gzip Archive (Creates a compressed backup file on the Host)
↓
External Storage (S3 / Offsite storage)

Because the volume is managed by Docker, the safest way to back it up is to spin up a tiny, temporary Linux container (like ubuntu or alpine), mount the volume to it, and compress the contents.

Terminal window
# Back up a named volume 'app_uploads' to the current host directory
docker run --rm \
-v app_uploads:/volume-data \
-v $(pwd):/backup-dest \
alpine tar cvzf /backup-dest/uploads_backup.tar.gz -C /volume-data .

To restore, reverse the process. Create a fresh named volume, and extract the archive into it.

Terminal window
docker run --rm \
-v app_new_uploads:/volume-data \
-v $(pwd):/backup-dest \
alpine tar xvzf /backup-dest/uploads_backup.tar.gz -C /volume-data

Creating a backup is only step one. A true Disaster Recovery plan ensures your business survives when the entire data center catches fire.

Never store backups exclusively on the same server that runs the application. If the server dies, both the application and the backup are lost.

  • 3 Copies of Data: The live data + 2 backups.
  • 2 Different Media: E.g., SSD storage and Cloud Object Storage.
  • 1 Offsite Copy: Push your compressed .tar.gz and .sql dumps to an offsite cloud provider (like AWS S3 or Backblaze B2) using an automated cron job.

A backup is completely useless if it is corrupted and cannot be restored. You must periodically perform “Fire Drills.” Spin up a fresh Docker stack on a local machine or testing server, pull the latest backup from S3, and verify that the application successfully boots and reads the restored data.

“How much data are you willing to lose?”

  • If your cron job backs up the database every 24 hours (at midnight), and the server crashes at 11:59 PM, you just lost 24 hours of customer data. Your RPO is 24 hours. For critical applications, backups must happen more frequently (e.g., hourly, or using continuous replication).

“How fast can you bring the system back online?”

  • Because Docker utilizes Infrastructure as Code (compose.yaml), your RTO is incredibly fast. If a server dies, you can provision a new VPS, run git clone, execute docker compose up, download the database backup from S3, and restore it. What used to take days of manual server configuration now takes minutes.