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.gz
in 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