TRAFEX TRAFEX Consultancy Consultancy
WordPress Docker image

WordPress Docker image

August 16, 2016

I’m moving all my sites and applications to Docker containers. The main reason is that it makes maintenance easier when everything an application needs is included in a single container. I can run everything on one server or split it up over multiple servers when I need more resources or high availability.

Requirements

To containerize WordPress, I couldn’t find a container on hub.docker.com that fit my needs. So I had to build my own with the following requirements in mind:

  • Alpine Linux; A great distribution for containers because it’s so small and only includes the bare minimum which also makes it more secure.
  • PHP 8.3; The newest major version of PHP which has the best performance and lowest memory usage.
  • Nginx & PHP-FPM: A golden combination that performs really well and has a small memory footprint thanks to the ‘ondemand’ process manager of PHP-FPM.
  • Flexible configuration; I want a flexible wp-config.php configuration to avoid having to change the container when the configuration needs to change.
  • No SSL support; I’m using Amazon Cloudfront to terminate SSL, and there are enough other systems and CDN’s that can do that for you, no need to include it in the container.
  • Simple, not too much magic; The official WordPress container has a bash script that does all kinds of magic to bootstrap WordPress. I like to keep it simple and focused on my use-case.

Dockerfile

If you have a look at the Dockerfile, you’ll see the following things happening:

  1. It installs all the required packages and PHP extensions for WordPress
  2. It copies the Nginx & PHP-FPM configuration into the container
  3. It copies the supervisord configuration which makes sure both Nginx and PHP-FPM are started when you run the container
  4. It creates the volume for wp-content. That folder has all the files you want to persist.
  5. It downloads and extracts the WordPress archive to /usr/src/wordpress
  6. It copies the wp-config.php into the container which relies on environment variables to configure WordPress
  7. It copies the wp-secrets.php into the container which will hold the secrets and salts
  8. It copies entrypoint.sh into the container for bootstrapping WordPress for the first time
  9. It starts supervisord which will start Nginx & PHP-FPM

Entrypoint

entrypoint.sh does only two things, and only when you start the container for the first time.

  1. When the wp-content volume is empty, it copies the wp-content folder from the WordPress install to make sure you’ve got the default theme and plugins.
  2. When the wp-content volume is empty, it generates the secrets & salts in wp-secrets.php with the API WordPress provides.

Use it yourself

The image is available on Docker Hub. Thanks to Alpine Linux, it’s only +/- 50MB.

Docker Pulls

I’ve also included a docker-compose.yml to have a working WordPress website with one simple command: docker-compose up

If you have any questions or feedback, just create an issue on GitHub

GitHub Repo stars

Go back

Related content

Projects

PHP Nginx Docker image

Example PHP-FPM 8.3 & Nginx 1.26 setup for Docker, built on Alpine Linux.

Read More

Articles

Caching WordPress with Nginx

I’m running a WordPress website on a small VPS with the help of Nginx and PHP-FPM. But I noticed the site was not that fast as I wanted it to be. This is mainly caused by the little resources this VPS has. So every request needs to be as efficient as possible. To make that possible, I want to cache the requests with Nginx to minimize the requests that needs to be handled by PHP-FPM.

Read More