Convert WordPress Server into Docker Containers (Part 1)

In this post, we are going to discuss how to migrate a traditional WordPress + MySql or MariaDB into Docker containers. This post is very involved and requires knowledge spanning a wide spectrum of technologies. This post assumes that you know, or can find out about opening firewall ports, UNIX/Linux commands, etc. And only focus on the necessary step to convert WP+DB into Docker containers. Now, let’s discuss the prerequisites:

Prerequisites:

  • Obviously an instance of WordPress and a MySQl or MariaDB to migrate. Should the database be different like PostgreSQL, or something else, the steps will still apply, but the specific technologies will change.
  • Ability if needed, to change the DNS record of the instance you are migrating. This step might be required and it’s based on how your instance of WordPress is running. If you need this step, but don’t understand how is done, please get in touch with someone that understands DNS migrations.
  • A Linux server with Docker and Docker Compose installed. The server can be a Windows server, but the commands listed in this post are Linux. To make it work on Windows, you will have to convert the commands into either PowerShell or traditional DOS style commands. For docker installation, reference this link:https://docs.docker.com/engine/install/. And for docker-compose installation, follow this link:https://docs.docker.com/compose/install/.
  • This post assumes docker-compose is in the PATH system variable. Should you need to add docker-compose to the system PATH. Search your system documentation on how to do that.
  • Basic knowledge on how to spawn a system shell, either via SSH or locally. This posts assumes that BASH is used. If any other shell is used, the commands may need conversion.
  • Authentication information to the admin portion of the traditional WordPress installation and to the database.
  • Access to the directory structure if the traditional WordPress installation to be able to export files.
  • Access to the phpmyadmin or command line utility to be able to export MySQL/MariaDB database.

Now that the prerequisites are out of the way, let’s proceed to the first step, to export the current MySQL/MariaDB database.

On the other hand, if this installation does not have a migration, but only brand new WordPress site + DB, simply skip part 1, begin in part 2, and exclude all the migration steps. I will indicate which steps can be skipped in case of a brand new site.

Export MySQL/MariaDB Database

If you are reading this post, you should know which database your WordPress installation is connecting to; just in case you do not, look at the wp-config.php file, one of the defined fields is:

define(‘DB_NAME’, ‘thedbname’);

We are going to export that database. In this post, I will refer to that database as wordpressdb. In the same file, you should find other definitions for DB_USER, DB_PASSWORD, and DB_HOST. Note those down, you will have an option to reuse them them in the new installation.

My preferred way to export a MySQL/MariaDB database is to use the mysqldump utility. This utility exports the entire db into a file, which is easy to transport and import into a new instance. Assuming you know the user, password, have a shell with the proper permission to access the db client, all you have to type is:

mysqldump -u {the user} wordpressdb -p > wordpressdb.sql

The command will prompt you for the password, which you should know, and will dump the database wordpressdb into a the file wordpressdb.sql. If you want to use another filename or database, please use it instead.

You might want to compress the file to save space and time to copy it with the command:

gzip wordpress.sql

At this point, the database is exported, copy the file wordpressdb.sql.gz to the destination server hosting the docker daemon. Should you need additional step-by-step instructions on how to export MySQL/MariaDB database, you can use the following this link. Now, on to the next step, export the WordPress directory.

Export The WordPress Directory

Time to export the actual WordPress Installation directory. In Docker, we are not going to use the while directory, but only the customized parts, more specifically, the wp-content directory which contains the WordPress customizations applied. This way maintenance and WordPress upgrades will happen without much effort. You will see all this when we spawn the container images. For now let tar the actual directory, normally in Linux, WordPress is installed in /var/www/html, but this might change based on how you installed. This post assumes that /var/www/html is used. Any different might require additional steps to modify the URL which is beyond the scope of this article. However, should you need the steps, I will provide a general guidance.

First step is to open a terminal and navigate to /var/www/html directory, and type the following command:

tar cvfz /tmp/wordpress-export.tgz *

the command above simply creates a tarball of all the directory content. Should you need clarification on tarballs, I wrote an article which explains the tar command here.

Now that we have the export file (wordpress-export.tgz) we can copy it to the server which ha the docker daemon, the same way we did it for the mysql export file.

This concludes part 1. In part 2, we are going to discuss, creating the new WordPress user, creating the docker images, and running the containers with the exported data.

If you are ready continue to part 2 here.

Leave a Reply