Docker is a great tool for developers. It allows you to package up your application and all its dependencies into a single file, called a container. This makes it easy to share your application with others and to run it on different machines.
One of the great things about Docker is that it makes it easy to backup and restore your data. In this blog post, we’ll show you how to use Docker’s built-in backup and restore commands to backup and restore your database.
First, we’ll need to create a backup of our database. We can do this using the docker exec command. This command will allow us to run commands inside our container.
To create a backup, we’ll run the following command:
docker exec CONTAINER /usr/bin/mysqldump -u USERNAME -pPASSWORD DATABASE > backup.sql
Replace CONTAINER with the name of your container, USERNAME with your database username, PASSWORD with your database password, and DATABASE with the name of your database. This will create a file called backup.sql in your current directory.
Now that we have a backup, we can restore it using the docker exec command.
To restore our backup, we’ll run the following command:
docker exec -i CONTAINER /usr/bin/mysql -u USERNAME -pPASSWORD DATABASE < backup.sql
Replace CONTAINER with the name of your container, USERNAME with your database username, PASSWORD with your database password, and DATABASE with the name of your database.
This will restore your database from the backup.sql file. That’s all there is to it! Backing up and restoring your Docker database is easy using the docker exec command.