Getting Started with WordPress

Learn the steps we will be following to use WordPress with Docker.

WordPress

WordPress is a PHP and MySQL-based open-source Content Management System first launched in 2003. It powers 40% of all websites, is supported by many web hosts, is easy to set-up, and offers thousands of free themes and plugins.

If you’re a web developer, you’ve probably encountered WordPress at some point in your career. Even modern static sites can adopt WordPress as a headless CMS where it is used to edit the content that is fed into a site generator.

Even if you don’t use WordPress, the concepts demonstrated in this chapter show how to execute an application in a Docker container. This is essential for development, but the technique is rarely discussed in Docker tutorials.

WordPress requirements

To develop a WordPress-powered site, you must install:

  1. A web server, typically Apache.
  2. The PHP runtime and extensions, then configure the web server accordingly.
  3. MySQL or MariaDB, then define a new database for WordPress use.
  4. WordPress itself, plus any required themes and plugins.

Tools such as XAMPPIt is the most popular PHP development environment. can ease some of that effort but, with Docker, you’ll be running WordPress and developing code within minutes.

Docker configuration plan

You will be pulling two Docker images:

  1. wordpress
  2. mysql:5

Using the latest image for wordpress is a good choice. It provides the stable versions of Debian Linux, Apache, PHP, and WordPress. There are more lightweight alpine images, but they do not match production hosting environments and may not work as expected. The containers launched from these images will be added to a wpnet Docker network.

Docker volumes

Two Docker volumes will be created:

  1. The wpdata for the MySQL database that are mounted to /var/lib/mysql in the MySQL container.
  2. The wpfiles for WordPress application files that are mounted to the Apache server root directory at /var/www/html in the WordPress container.

📌 Some developers will claim that mounting the wpfiles volume is an anti-pattern because the WordPress container is no longer stateless. This is true, but WordPress is not a stateless application. Docker is being used to emulate a live server environment for development purposes. You are unlikely to use the same configuration in production.

Mounting the wpfiles volume has a number of benefits:

  1. Docker start-up is faster. There’s no need to copy the core WordPress files every time the container is launched.

  2. The WordPress application can be updated automatically. This would happen on live installations, so replicating it during development is useful.

📌 If you choose not to mount a wpfiles volume, you can run docker pull wordpress every so often to download the latest application image.

It should not be necessary to inspect or modify files stored on either volume.

Development directory bind mount

A wp-content subdirectory will be created in the project directory. This contains all plugins, themes, and uploaded assets and mounts to /var/www/html/wp-content in the WordPress container.

WordPress developers should only ever create and modify files in the wp-content directory.

📌 There’s nothing to stop you from editing core WordPress files, but the changes would disappear the moment the application is updated.

As a bonus, this makes it easier to commit wp-content files to Git or other repositories. It is the only WordPress folder on your host PC.

Get hands-on with 1400+ tech skills courses.