Backup MySql Databases Running in Containers

Backing up MySql or mariaDB databases is a straight forward task. The task is similar in backing up a regular database located in a Linux server. We would issue the following command:

mysqldump -u {USER} -p{PASSWORD} {DATABASE} > {THE BACKUP FILE}

On a Linux server, the command would look something similar to:

mysqldump -u root -pthesecretpassword wordpress_db > /tmp/wordpress_db.sql

The command will simply export all the SQL statements needed to possibly re-create from an empty database.

If MySql runs in a container, we would do the same exact command, but we will have to issues in the running container, in order to do so, we would need the container ID, retrievable with the command: docker container ls.

Once we have the container id, we would simply run the following command:

docker exec {CONTAINERID} mysqldump u {USER} -p{PASSWORD} {DATABASE} > {THE BACKUP FILE}

The command above will produce the same exact file as the previous command.

As you can see, it’s very simple to backup databases running in a container.

I hope you liked this little tutorial. Enjoy it and type away!!!

Leave a Reply