WordPress on Docker

Migrating to WordPress to Docker

WordPress is the world’s most popular blogging platform by a long shot. It combines extensibility with ease of use to create powerful software for managing all sorts of websites. The WordPress ecosystem consists of thousands of themes and plug-ins that users can mix and match to create unique websites for many purposes. WordPress is quite mature and has been adapted to all sorts of platforms and operating systems given that it runs on PHP. One of the more recent platforms that WordPress has been migrated to is Docker.

Docker provides a prebuilt image for WordPress that comes with everything needed to get WordPress up and running, While this image is designed for new installs. It can easily be adapted to existing installs of WordPress too.

A simple WordPress install usually has WordPress running on a web server of some kind that supports PHP. The app then connects to a MySQL database for storing data. Most of the actual file content (uploads, themes, and plugins) is stored in a folder in the WordPress directory called wp-content. This content is usually unique to the site while the rest of the content is the WordPress app itself. Similarly, MySQL consists of the Database Engine and files that contain the actual data used by the Engine.

To install WordPress on Docker, it’s generally best to store the file content and database for the site outside a container. Containers by design are intended to be used and disposed of rapidly such that nothing that needs to be persisted is stored in the the container itself. To handle persistent data, Docker uses volumes to do this, such that persistent data can have a life longer than that typical of containers. These volumes can be mounted inside a container, and when a container is removed the data won’t be removed.

WordPress on Docker
WordPress on Docker

This guide is designed to serve as a general purpose guide for getting WordPress onto Docker. Naturally, there will be holes given the wide array of possible installation mechanisms for WordPress.*

**The Dockerfiles and folders referenced in this guide can be found here.**

MySQL

Docker Hub provides many versions of MySQL that are ready to run. These images have the ability to import a database when a container starts for the first time. This feature is useful for migrating data to a containerized instance of MySQL. This step is only necessary if you intend to use a container for the database as well. Many services running in containers do not necessarily use containerized databases, albeit it is possible and acceptable, particularly for smaller installations.

  1. Backup the source database.
    • If you have access to the console on the machine running MySQL, you can use mysqldump.
     mysqldump --databases wordpress-database -u username -p > wordpress.sql
    

    This will prompt for a password for the username with permissions to dump the database. Enter the password and it will create a .sql file. Replace wordpress-database with the name of the WordPress database and username with the user that can dump the database. Download the file to your local computer once you’re finished.

    • If you don’t have console access, odds are you have access through phpMyAdmin. This portal allows you to do all kinds of things with MySQL databases.
      1. Log in to the phpMyAdmin
      2. Click the Export tab
      3. From the database drop-down (left navigation), select the WordPress database.
      4. Choose Quick export.
      5. For the format select SQL.
      6. Click the Go button.
      7. Save the file – This will download a .sql file with the name of the database.
  2. Move the .sql to the same folder where the Dockerfile for the MySQL image is located.
  3. Edit the Dockerfile. Change the second line of the file so that the first parameter (yourdatabase.sql) in the ADD statement matches the name of the database from from the backup. After editing, save he Dockerfile.
     ADD yourdatabase.sql /docker-entrypoint-initdb.d/wordpress.sql
    
  4. Build the image. With the Docker client connected, run Docker build. This will create a container image in a local image registry from the generic MySQL image on Docker Hub.
     docker build --no-cache --tag wp-mysql .
    
  5. Create a folder on the Docker host for the MySQL data path. This will be used for the data persistence.
     mkdir /var/some/path/
    
  6. Create a Docker network. This network will be used by WordPress and MySQL internally on Docker.
     docker network create wp-net
    
  7. Run the image as a container. Notice the -v parameter maps the created folder to the mysql folder in the container. The container will now write persistent data to the host’s file system outside of the container. Also, note the MYSQL_ROOT_PASSWORD. Set this password to whatever you use for the root on MySQL. Note the –net parameter that specifies the created network .
     docker run -dit --name wp-mysql -v /var/some/path:/var/lib/mysql --net wp-net -h wp-mysql -e MYSQL_ROOT_PASSWORD=yourpassword wp-mysql
    

Once started, the container is ready to use.

WordPress

This guide uses a custom Dockerfile that uses the WordPress image from Dockerhub. It can be found in the wordpress folder with the scripts downloaded for this guide.

  1. The first thing that you will need to do is download the entire WordPress site from the existing host. Usually, everything in WordPress is stored under one folder. That folder contains a file called wp-config.php that resides in the root folder. Copy it and everything in the folder along with sub-folders. This can be done any number of ways through an online portal, FTP, SCP, or other sorts of file copying software. On many servers hosting WordPress, the folder is /var/www/html. A web hosting company may have it in an HTML folder accessible through a control panel of sort.
  2. Once the site is downloaded, move files into the wordpress folder where the Dockerfile for WordPress resides.
  3. Move the contents wp-content folder out of the wordpress folder into the wp-content folder under in the folder that contains the Dockerfile.
  4. Open the wp-config.php file in the wordpress folder. Change the DB-USER value to root, the DB_PASSWORD value to the password used in setting up the database, and DB_HOST to wp-mysql. Then save the file.
     /** MySQL database username */
     define('DB_USER', 'root');
    
     /** MySQL database password */
     define('DB_PASSWORD', 'yourpassword');
    
     /** MySQL hostname */
     define('DB_HOST', 'wp-mysql');
    
  5. Move the wp-content file to the Dockerhost. There are any number of ways to do this, including a straight copy, mounting a remote share, copying to an external drive and mounting the drive on the Docker host, among many other methods. Additionally, Docker provides a number of volume drivers that can be used to access a variety of different kinds of data. Moving the data may not even be necessary of the data is on the same host already. The bottom line is that regardless of how the data is moved and accessed by the host, there needs to be a path to the data, such as /var/wp-content or some other path. This path will be mounted in the WordPress container as a volume.
  6. Build the WordPress image. This will pull the generic image from Docker Hub and move the WordPress app into the container.
     docker build --no-cache --tag wp-docker .
    
  7. Run the container. the -v flag contains the path created in step 5. The –net points to the Docker network created when creating the MySQL container.
     docker run -dit --name wordpress -v /path/to/wp-content:/var/www/html/wp-content --net wp-net -p 80:80 wp-docker
    

    Once the container is running, you’ll need to update the DNS record for your website to point to the Docker host’s IP address. Once finished, you can point your browser to the site and see it running in a container.

Conclusion

This guide walks through the necessary steps to migrate an existing WordPress instance into a container. The entire process is fairly straight forward, and once it’s up and running, it’s simple to maintain and use just like it was on a server without the headaches of having to run the server. Happy blogging!

7 comments on “Migrating to WordPress to DockerAdd yours →

  1. I’m very new to docker. Using these instructions, wordpress does not use the database that was exported. WordPress does start, however the site behaves as if this is a new install and asks for the language and database login, user, etc. as though you were using a blank wordpress install. Also, other than going to localhost, is there some other network address to connect to. Ifconfig shows virtual ethernet addresses but with ipv6 addresses, not ipv4.
    The wordpress site does not start up as the existing site that I exported from the beginning steps.
    What am I missing?

    1. Look at your wp-config file to make sure that the database settings are pointed to the container instance of MySQL. If it is looking like a new install, I suspect it may be that the app files didn’t get copied to the container correctly. I’d check that too.

    2. If you could, run

      docker cp wordpress:/var/www/html/wp-config.php wp-config.php

      Then run

      cat wp-config.php. Obfuscate the passwords, if any.

      If you get an error, it’s probably because something didn’t copy right into the container.

Leave a Reply

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

four × 4 =