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?
- Docker Volumes: Contents of named volumes managed by Docker.
- Bind Mounts: Data mapped to host folders.
- Database Dumps: Logical (SQL) dumps for easy portability & restore.
- 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 .
- This creates a
pgdata_backup.tar.gzin your current directory. - More on managing Docker volumes
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
- Backup, restore, or migrate data volumes | Docker Docs
- Docker Compose file reference
- Best practices for backing up Docker containers – Docker Blog
- Backing up Docker volumes to S3 (article)
Leave a Reply