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:
- It installs all the required packages and PHP extensions for WordPress
- It copies the Nginx & PHP-FPM configuration into the container
- It copies the supervisord configuration which makes sure both Nginx and PHP-FPM are started when you run the container
- It creates the volume for wp-content. That folder has all the files you want to persist.
- It downloads and extracts the WordPress archive to /usr/src/wordpress
- It copies the wp-config.php into the container which relies on environment variables to configure WordPress
- It copies the wp-secrets.php into the container which will hold the secrets and salts
- It copies entrypoint.sh into the container for bootstrapping WordPress for the first time
- 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.
- 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.
- 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.
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
Go back