Mysqldump from Docker container
docker exec -i mysql_container mysqldump -uroot -proot --databases database_name --skip-comments > /path/to/my/dump.sql
This will generate a
dump.sqlfile in your host machine. Awesome, eh?
--compacton your dump. This will make MySQL check your constraints which will cause troubles when reading your file (damm you MySQL). And don't use
--forceto fix this scenario: recreate your dump without
You can execute the same command for both Docker and Docker Compose scenarios 😉
To import again your dump in a MySQL container, the best approach is to have another container with your dump files added during the build process. In my experience, the usage of making Docker read the dump from your host right to the guest caused troubles (errors like
socket.error: [Errno 32] Broken pipeand the terrific
ValueError: file descriptor cannot be a negative integer (-1)described here). So, in order to import again your dump:
- From Docker Compose:
docker-compose exec mysql_service /bin/bash -c 'mysql -uroot -proot < /path/to/my/dump.sql'
- From Docker:
docker exec mysql_container /bin/bash -c 'mysql -uroot -proot < /path/to/my/dump.sql'