As with any essential software project back up database data, as well as restore these backups, your MySQL or Mariadb databases, as well as PostgreSQL.

When you are working with the developmental environment of your Laravel PHP projects in Laradock Docker containers, it is important to understand how the directories and files structure is configured in Docker.

Where are the database files at Docker Laradock

In Docker the files manipulated in their containers may exist only during the time of execution, or have shared volumes with the host operating system.

It is important to understand that the dock containers are usually temporary, that is, they are performed only during the time required for the control control in the container to be completed. Thus, by default, all data created within the container is only available while it is running.That is why we need to set up shared volumes on Docker.

In Linux Ubuntu and Mint, Laradock the configuration of database service container volumes, by default, points to the .laradock/data directory, which is a subdirectory in its user root directory.

This configuration is in the .env file, in the variable data_path_host = ~/.laradock/data. Therefore, you can change the default path of your container data to the most appropriate to your context. Let’s go considering the standard path.

In this way, the MySQL files at Docker Laradock are in the ~/.laradock/data/mysql directory, ~/.laradock/data/mariadb and PostgreSQL in ~/.laradock/data/.postgres. Other database services will follow the same logic of volume sharing.

Backup (Dump) of MySQL at Docker Laradock

Therefore, to backup or fump of a MySQL database at Docker Laradock, you must execute the command mysqldump within the correct directory in the container, pointing out the destination for a directory on your host operating system. For that we will call the command docker exec:

docker exec laradock_mysql_1 /usr/bin/mysqldump -u root --password=root mydatabase > backup.sql

In the command line above:

  1. We indicate that the execution happens in the laradock_mysql_1 container, this configuration can change according to the name you have defined for your containers in the Laradock configuration
  2. We executed the command mysqldump
  3. define the root user with the parameter -u
  4. the password with the parameter --password
  5. The name of the database we want to do the dump: mydatabase
  6. and the destination in our host system, in the example, in the directory where we are executing the docker exec

For Mariadb we can perform the container name adjustments and the command of Dump:

docker exec laradock_mariadb_1 /usr/bin/mariadb-dump -u root --password=root mydatabase > backup.sql

If necessary add the host and server port using the parameters -h e -P:

docker exec laradock_mysql_1 /usr/bin/mysqldump -h 0.0.0.0 -P 3306 -u root --password=root mydatabase > backup.sql

In the example above the host IP is 0.0.0.0, if it does not work use 127.0.0.0.

Restoring a MySQL backup at Docker Laradock

To restore a backup file, made with mysqldump, or even run a file with SQL commands into a MySQL database in a Laradock Docker container, run the following command line:

docker exec -i laradock_mysql_1 mysql -u root --password=root mydatabase < backup.sql

In the command line above:

  1. We indicate that the execution happens in the laradock_mysql_1 container, this configuration can change according to the name you have defined for your containers in the Laradock configuration
  2. We executed the command mysql
  3. define the root user with the parameter -u
  4. the password with the parameter --password, The password can also be indicated with -p, example -proot
  5. The name of the database to where we want to execute the restore: mydatabase
  6. The path of the file with the SQL commands, this file is in our host operating system.

For Mariadb we can perform the container name adjustments and the command:

docker exec -i laradock_Mariadb_1 mariadb -u root --password=root mydatabase < backup.sql

In the restoration command it is also possible, if necessary, to add the host and server port using the parameters -h and -p, as in the example of dump with these parameters.

References

Gist 1

Gist 2