WordPress Docker container

WordPress Docker container on hub.docker.com

I’m moving all my sites and applications to Docker containers.
The main reason is because it makes the 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 HA.
I’m currently provisioning everything with Ansible, but I’m looking to solutions that are focused on container orchestration.

Requirements

To containerize WordPress, I couldn’t find a container on hub.docker.com that fits 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 7.0; The newest major version of PHP is so much faster and uses less memory than 5.x.
  • Nginx & PHP-FPM: A golden combination that performance 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 who can do that for you, no need to include it in the container.
  • Simple, not to much magic; The official WordPress container has a bash script that does all kind 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 uses the 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 the entrypoint.sh into the container for bootstrapping WordPress the first time
  9. It starts supervisord which will start Nginx & PHP-FPM

Entrypoint

The 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 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 +/- 46MB.

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 a issue on GitHub


Leave a Reply