Dockerize an Existing WordPress Installation

Docker is an unbelievably popular technology right now. It’s extremely flexible and can be used at every stage of an application’s lifecycle. In fact, a Docker container and the application running inside it can move together through the application lifecycle all the way to production. In this post, we’re going to cover some of the core concepts of Docker by looking at how you can migrate an existing WordPress installation into Docker (or “Dockerize” it). For the purposes of this article, I’ll be creating Dockerized copy of this very blog. Before we get started, Let’s get started!

First, I’d like to go over a few assumptions I’m making:

1. You have already installed Docker
2. You have a basic understanding of MySQL and can make your own database backups.
3. You have made backups of your existing WordPress database and files. If not, please create these backups before continuing
4. Those backups are accessible by the Docker host.

Lastly, please note that any values inside brackets <> are placeholders and should be replaced with real values.

Step 1: Pull pre-built MySQL and WordPress containers from Docker Hub
Pull WordPress and MySQL containers with the following commands:

(Note: You can also build your own images if you need to integrate customizations, etc.)

Step 2: Run the MySQL container with custom environment variables
Run the MySQL image and provide values for the MYSQL_USER, MYSQL_PASSWORD, and MYSQL_DATABASE environment variables which match your source MySQL instance. For example, if your source database was named wp, your WordPress database user was wp_user, and your WordPress database user’s password was wordpress_is_awesome your docker run command would be:

Step 3: Move your database backup into the container
Transfer your database backup into the container with the following command:

Step 4: Attach to the MySQL container and import the database backup
Attach to the running container with the following command:

and restore your existing database backup into the newly created database with the following command:

Once the backup has finished restoring, you should connect to MySQL as the root user and grant appropriate permissions on the database to the MYSQL_USER account you defined in the earlier docker run command. This step is necessary since the MySQL instance running inside the container doesn’t have the same privilege tables as your source MySQL instance.

Step 5: Run the WordPress container and link it to the MySQL container
Run the WordPress container and link it to the MySQL container my with the following command:

Once the container has started, connect to MySQL database server (inside the MySQL container) as the root user and drop the database that was created during the WordPress container’s initialization. This auto-generated database isn’t needed since you restored your database into the MySQL container in an earlier step.

Step 6: Replace WordPress files
Replace the WordPress files that were downloaded to the persistent volume during the WordPress container’s initialization with the files backed up from the existing WordPress installation with the following command:

Make sure the wp-config.php file is included in the files transferred into the container/persistent volume. Since this file already contains connection settings from your “old” WordPress instance, it shouldn’t require much editing beyond updating the DB_HOST variable to point my mysql (the name of the linked MySQL database container). For example:

Special note: If you’re dockerize’ing your WordPress installation and changing the site URL simultaneously (as I was during the project that inspired this post), refer to the notes below for a couple important important pieces of information.

  • You may see the following error: “You appear to have already installed WordPress. To reinstall please clear your old database tables first.” when attempting to login to your site for the first time after migrating it. This is expected since the database you’re connecting to already contains WordPress tables. Simply click Log in to continue.
  • After clicking Log in you should be redirected to the login page of the old WordPress installation (or a 404 page if it’s no longer functional). To update the site URL edit the wp-config.php file and add the following lines:

Those are the major steps required to move an existing WordPress installation into Docker containers. From here, you would simply update your DNS records (assuming your Docker host was running on an IP address different than that of your source web server) and login to your admin dashboard to make sure all of your plugins, media, etc were successfully migrated and are working correctly. You’ll notice that I didn’t create a persistent volume for the MySQL container. That’s intentional. I wanted to show how files can be copied either directly between host and container using docker cp or indirectly by copying directly to the persistent volume. In a production scenario you will absolutely want to create a persistent volume for your MySQL container as well as your WordPress container.


Leave a Reply

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