Introduction
During the development and deployment of PHP applications, data migration is often a necessary process. Migrating existing data from one environment to another requires ensuring data integrity and maintaining efficiency and stability. Using Docker Compose, Nginx, and MariaDB together can simplify this task. In this article, we will walk through the steps and provide configuration examples to show how these tools can be used for PHP application data migration.
Prerequisites
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define and start services using a single configuration file. Nginx, acting as a reverse proxy server, forwards HTTP requests to the appropriate PHP container. MariaDB is the database service that stores application data. The first step is to create a Docker Compose file to define our services.
Docker Compose Configuration
In the Docker Compose file, we need to define two services: Nginx and MariaDB. Here's a basic configuration example:
version: '3'
services:
nginx:
image: nginx
ports:
- 80:80
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf
mariadb:
image: mariadb:10.5
environment:
- MYSQL_ROOT_PASSWORD=secret
volumes:
- ./data:/var/lib/mysql
In this configuration, the Nginx service uses the official Nginx image and maps the container's 80 port to the host's 80 port. The MariaDB service uses the official MariaDB image and sets the root password via an environment variable. We also mount a directory to persist the database data.
Nginx Configuration
Next, we need to configure Nginx to forward HTTP requests to the PHP application container. Here’s a simplified `nginx.conf` configuration example:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://php-app;
proxy_set_header Host $host;
}
}
Here, we define a proxy server called php-app and forward all HTTP requests to that server. The proxy_set_header directive is used to pass the Host header to the proxy server.
Starting the Services
Once the configuration is done, you can start the services with the following command:
docker-compose up -d
This command will start the containers for Nginx and MariaDB. You can verify that Nginx is working correctly by visiting http://localhost.
Data Migration
Suppose you already have an exported MySQL database file, `backup.sql`, and you want to import it into the MariaDB container. You can run the following command to perform the import:
docker exec -i <mariadb_container_name> mysql -uroot -p<password> < backup.sql
In this command, is the name of your MariaDB container, is the root user's password, and backup.sql is the database export file.
Scheduled Backups
To regularly back up your database, you can create a simple shell script:
#!/bin/bash
docker exec <mariadb_container_name> mysqldump -uroot -p<password> <database_name> > backup.sql
In this script, is the name of the MariaDB container, is the root password, and is the name of the database you want to back up. This script will create a backup.sql file with the database dump.
Conclusion
Using Docker Compose, Nginx, and MariaDB allows you to easily containerize your PHP application and database while simplifying data migration and backup processes. By containerizing services, deployment and migration become more efficient and stable, with Nginx facilitating HTTP request forwarding between containers.