Getting Started with WordPress
Learn the steps we will be following to use WordPress with Docker.
We'll cover the following
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:
- A web server, typically Apache.
- The PHP runtime and extensions, then configure the web server accordingly.
- MySQL or MariaDB, then define a new database for WordPress use.
- WordPress itself, plus any required themes and plugins.
Tools such as
Docker configuration plan
You will be pulling two Docker images:
wordpress
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:
- The
wpdata
for the MySQL database that are mounted to/var/lib/mysql
in the MySQL container. - 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:
-
Docker start-up is faster. There’s no need to copy the core WordPress files every time the container is launched.
-
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 rundocker 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 1200+ tech skills courses.