Dockerize a WordPress Website, PhpMyAdmin and its Database.

“Dockerizing” a WordPress website and its database simply means the process of packaging the WordPress application, its dependencies, and the database into Docker containers. Which allows you to easily isolate and run the website and its dependencies in a consistent environment across different systems.

Steps

1- Back up your website:

Backing up a WordPress website involves creating copies of your website’s files, databases, and configurations to ensure that you can restore the site to a previous state in case of data loss, website errors, or other issues. There are different components to consider when creating a backup:

a. Files:

Backup all WordPress files, including core files, themes, plugins, and media uploads. This ensures you can restore the entire website’s structure and content.

b. Database:

Backup the WordPress database, which contains essential information such as posts, pages, comments, settings, and user data.

c. Configurations and Settings:

Save any configuration files or settings specific to your website, including wp-config.php and other custom configuration files.

Manual Backups:

a. Files:

  • Use an FTP client or a file manager in your hosting control panel to download all files from your WordPress root directory to your local computer.

b. Database:

  • Access your database using a tool like phpMyAdmin or command-line tools.
  • Export the entire database or select specific tables associated with your WordPress installation.
  • Save the exported SQL file to your local computer.

c. Configurations and Settings:

  • Download your wp-config.php file and any other custom configuration files.

2- The Docker Compose file:

version: '3.1'
services:
wordpress:
image: wordpress:php7.2-apache
ports:
- "8000:80"
environment:
WORDPRESS_DB_HOST: mysql
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: root
WORDPRESS_DB_NAME: wordpress_db
volumes:
- ./website:/var/www/html
mysql:
image: mysql:5.7
environment:
MYSQL_DATABASE: wordpress_db
MYSQL_ROOT_PASSWORD: root
ports:
- "3306:3306"
volumes:
- ./data:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- mysql
ports:
- "8081:80"
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: root
volumes:
website:
data:

This Docker Compose setup allows you to run a WordPress website with a MySQL database and manage the database using phpMyAdmin. The volumes ensure that data is persisted even if containers are stopped or removed. To use it, save this configuration in a file named docker-compose.yml and run docker-compose up in the same directory.

WordPress Service (wordpress):

  • Image: It uses the official wordpress:php7.2-apache Docker image, which includes PHP 7.2 and Apache web server specifically configured for WordPress.
  • Ports: Maps port 8000 on the host machine to port 80 on the WordPress container, allowing access to the WordPress site via http://localhost:8000.
  • Environment Variables:
  • WORDPRESS_DB_HOSTWORDPRESS_DB_USERWORDPRESS_DB_PASSWORD, and WORDPRESS_DB_NAME set the database connection details for WordPress to use.
  • Volumes: Binds the ./website directory on the host to /var/www/html in the WordPress container. This ensures that the WordPress files are persistent and can be shared between the host and the container.

MySQL Service (mysql):

  • Image: Uses the official mysql:5.7 Docker image.
  • Environment Variables:
  • MYSQL_DATABASEMYSQL_ROOT_PASSWORD: Set up the MySQL database and root password.
  • Ports: Maps port 3306 on the host to port 3306 on the MySQL container, allowing external tools to connect to the MySQL database.
  • Volumes: Binds the ./data directory on the host to /var/lib/mysql in the MySQL container, ensuring persistent storage for MySQL data.

phpMyAdmin Service (phpmyadmin):

  • Image: Uses the official phpmyadmin/phpmyadmin Docker image.
  • Links: Establishes a link to the mysql service, allowing phpMyAdmin to connect to the MySQL database.
  • Ports: Maps port 8081 on the host to port 80 on the phpMyAdmin container, making phpMyAdmin accessible via http://localhost:8081.
  • Environment Variables:
  • PMA_HOST: Specifies the host for phpMyAdmin, linked to the MySQL service.

Volumes:

Defines two volumes:

  • website: Used for persisting WordPress files (where your backup of the wordpress files exists).
  • data: Used for persisting MySQL data.

Now run the following:

docker-compose up -d

3- Restore the DB backup

Access phpMyAdmin:

Open your web browser and go to the phpMyAdmin URL (e.g., http://localhost:8081 based on the Docker setup).

Login:

  • Log in with your MySQL username and password.

Select Database:

  • Choose the database where you want to restore the backup.

Import Backup:

  • Navigate to the “Import” tab.
  • Click on “Choose File” and select your SQL backup file.
  • Click “Go” to start the import process.

After completing these steps, your WordPress database should be restored to the state saved in the backup file. Ensure that your WordPress configuration (wp-config.php) reflects the correct database name, username, and password if you restored to a new database or made changes.

4- Fixing the links and options

Now in the backup, the links are still pointing to the old domain, but you might be hosting the dockerized app locally or in a new domian, we fix this by running wp search-replace command inside a WordPress Docker container.

Access the Container:

  • Use the following command to access the WordPress container:
docker exec -it your_wordpress_container_name_or_id /bin/bash

Replace your_wordpress_container_name_or_id with the actual name or ID of your WordPress container.

Navigate to WordPress Installation Directory:

  • Once inside the container, navigate to the WordPress installation directory. Typically, it’s located in /var/www/html/. Use the following command:
cd /var/www/html/

Install WP-CLI:

  • If WP-CLI is not installed, you can install it using the following commands:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar chmod +x wp-cli.phar mv wp-cli.phar /usr/local/bin/wp

Run wp search-replace:

  • Now, you can use the wp search-replace command. For example:
wp search-replace 'http://old-domain.com' 'http://new-domain.com' --skip-columns=guid --allow-root

Replace http://old-domain.com and http://new-domain.com with your old and new domain names. The --skip-columns=guid flag is used to avoid changing the GUID column, which is usually unnecessary and can be problematic if changed.

Exit the Container:

  • After running the command, exit the container:
exit

5- Fixing Files Acess

You might see the error message “Unable to create directory wp-content/uploads/2024/01. Is its parent directory writable by the server?” indicates that WordPress is having difficulty creating the specified directory within the wp-content/uploads directory. This issue is usually related to incorrect file or directory permissions.

Or simply you can’t customise the theme or install plugins due to incorrect file or directory permissions.

  • Use the following command to access the WordPress container:
docker exec -it your_wordpress_container_name_or_id /bin/bash

Make sure that the directories and files are owned by the correct user and group. The web server user (commonly, www-data on Apache) should have ownership:

chown -R www-data:www-data /path/to/your/wordpress/

Replace /path/to/your/wordpress with the actual path to your WordPress installation directory.

Now you should be good to go, you can access WordPress at http://localhost:8000 and phpMyAdmin at http://localhost:8080.

Benefits

Isolation:

  • Docker containers provide isolation, meaning each container encapsulates the application and its dependencies. This isolation ensures that the WordPress application and the database have their own environments, preventing conflicts with other applications running on the host system.

Portability:

  • Dockerized applications are highly portable. Once you’ve Dockerized your WordPress site, you can run it on any system that supports Docker without worrying about differences in underlying infrastructure. This makes it easy to deploy and scale your application across different environments.

Easy Deployment and Scaling:

  • Docker simplifies the deployment process. You can easily deploy your WordPress application and database containers using a single command. Scaling becomes more manageable as you can replicate containers horizontally to handle increased traffic or load.

Version Control:

  • Docker allows you to version control your application environments. By defining the environment in code (Dockerfile and docker-compose.yml), you can track changes over time and reproduce the same environment across different stages of development or on different machines.

Environment Variables and Configurations:

  • Docker Compose enables you to set environment variables for configurations, such as database connection details, WordPress salts, etc. This allows for flexible and secure configuration management.

Leave a Comment

Your email address will not be published. Required fields are marked *