šŸ”„ How to Safely Back Up & Restore Docker Compose Data

When you’re running apps with Docker Compose, your data is the heart and soul of your services—databases, media files, configurations, and more. Without a solid backup and restore plan, a simple mistake (or disk failure!) can lead to a world of pain. Here’s a step-by-step guide to properly back up and restore data from Docker Compose environments, the right way.


Why Back Up Docker Compose Volumes and Data?

By default, Docker containers are ephemeral. Persistent data lives in volumes or bind mounts—which need explicit backup. Databases (Postgres, MySQL), app uploads, and configs are often stored here.

If you lose a volume, your critical data is gone.


šŸ—‚ļø What Should You Back Up?

  1. Docker Volumes: Contents of named volumes managed by Docker.
  2. Bind Mounts: Data mapped to host folders.
  3. Database Dumps: Logical (SQL) dumps for easy portability & restore.
  4. Configs: Your docker-compose.yml, .env, TLS keys, and other setup files.

šŸš€ Step-by-Step: Back Up Docker Compose Data

1ļøāƒ£ Identify Your Data

First, look at your docker-compose.yml for volumes/bind mounts:

services:
  db:
    image: postgres
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

Here the named volume is pgdata.


2ļøāƒ£ Stop the Compose Stack (Recommended)

This ensures consistent backups (especially for databases):

docker compose down

3ļøāƒ£ Back Up Docker Volumes

List all local volumes:

docker volume ls

To back up a volume (e.g., pgdata), run:

docker run --rm 
  -v pgdata:/volume 
  -v $(pwd):/backup 
  busybox 
  tar czvf /backup/pgdata_backup.tar.gz -C /volume .

Repeat for each volume you want to back up.

Bind mounts?

Just copy the folder on the host:

cp -a /absolute/path/to/mount /your/backup/location

4ļøāƒ£ Logical Database Backups (Recommended for Portability)

For MySQL:

docker exec <db_container> mysqldump -u root -p<pass> <database> > mysql_backup.sql

For PostgreSQL:

docker exec -t <db_container> pg_dump -U <user> <db> > pg_backup.sql

šŸ’¾ Restoring Your Data

1ļøāƒ£ Restore a Docker Volume from Backup

Create the volume if needed:

docker volume create pgdata

Unpack the tarball:

docker run --rm 
  -v pgdata:/volume 
  -v $(pwd):/backup 
  busybox 
  tar xzvf /backup/pgdata_backup.tar.gz -C /volume

2ļøāƒ£ Restore a Bind Mount

Just reverse your earlier copy:

cp -a /your/backup/location /absolute/path/to/mount

3ļøāƒ£ Restore Database from Dump

For MySQL:

docker exec -i <db_container> mysql -u root -p<pass> <database> < mysql_backup.sql

For PostgreSQL:

cat pg_backup.sql | docker exec -i <db_container> psql -U <user> <db>

4ļøāƒ£ Bring the Stack Up

docker compose up -d

šŸ” Pro Tips

  • Automate backups! Use cron or a backup script.
  • Store backups offsite (cloud, external drive, etc).
  • Test your backups regularly! A backup you can’t restore isn’t a backup.

šŸ“š Resources



Comments

Leave a Reply

Your email address will not be published. Required fields are marked *