How to back up a MySQL Docker container (2026)
The correct way to back up a MySQL container is a logical dump, not a file copy. Run `mysqldump --all-databases --single-transaction --routines --triggers -uroot -p"$MYSQL_ROOT_PASSWORD"` inside the container and back up its output. Copying /var/lib/mysql while MySQL is writing produces an inconsistent snapshot; --single-transaction wraps the dump in one InnoDB transaction, capturing every table at a single consistent point without a write lock.
What Dockstash detects
| Env keys detected | MYSQL_ROOT_PASSWORD, MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD |
|---|---|
| Default port | 3306 |
| Live data paths (never copied live) | /var/lib/mysql |
| Example images | mysql:8, mysql:8.0, mysql:5.7, mysql |
The dump command
mysqldump --all-databases --single-transaction --routines --triggers -uroot -p"$MYSQL_ROOT_PASSWORD"The restore command
mysql -uroot -p"$MYSQL_ROOT_PASSWORD"--single-transaction wraps the dump in one InnoDB transaction, capturing every table at a single consistent point without a write lock.
Gotchas to avoid
- Never restic the live /var/lib/mysql directory — InnoDB tablespaces copied mid-write are inconsistent and will fail recovery.
- --single-transaction only gives a consistent snapshot for transactional (InnoDB) tables; MyISAM tables are not covered and need a lock.
- Add --routines and --triggers or stored procedures and triggers are silently dropped from the dump.
Do it in one click with Dockstash
Dockstash runs the exact dump above, restics it off-site, and drill-tests the restore automatically — no script to maintain.
Last updated: July 2026
Frequently asked questions
Why is --single-transaction important?
Without it, mysqldump reads tables one at a time and a write between tables produces an inconsistent dump. --single-transaction wraps everything in one InnoDB transaction so the whole dump reflects a single point in time, with no write lock.
Does the dump include stored procedures and triggers?
Only if you pass --routines and --triggers, which Dockstash includes. A default mysqldump omits them, which is a common cause of a "restored but broken" database.
Can I back up MySQL by copying /var/lib/mysql?
No. The InnoDB tablespace and redo logs are written continuously; a file copy mid-write is inconsistent. Dockstash runs mysqldump inside the container instead.
What if I use MyISAM tables?
--single-transaction does not make MyISAM consistent because it is non-transactional. If you rely on MyISAM, a brief lock (--lock-tables) is required — migrating those tables to InnoDB is the durable fix.