Installing WordPress with Docker Compose

A WordPress installation consists of two components – the WordPress website itself, and a MySQL database. Now while it’s certainly possible to have both of those running in one Docker container, the recommended way of working with Docker is to have one container per component. And with Docker Compose that’s really easy to achieve.

In fact, if we look at the official WordPress Docker image on Docker Hub, it gives us a docker-compose.yml file that we can cut and paste, simply replacing the password with one of our choosing. Here’s my docker-compose.yml file:

As you can see, it’s so simple that you can probably guess how it works even if you’ve never used Docker Compose before. We have two containers, or “services”, one which we call “wordpress”, which uses the “wordpress” image from Docker Hub, and one called “mysql” which uses the “mariadb” image from Docker Hub (MariaDb is a MySQL compatible database).

Each image configures one environment variable, which contains the root MySQL database password. And the “wordpress” container will expose it’s internal port 80 on port 8080, so when this is up and running, I’ll be able to connect to WordPress by using http://localhost:8080.

Docker Compose assumes that these two containers need to talk to each other, so automatically configures a network allowing them to communicate, using their container names.

So having created this docker-compose.yml file, what’s involved in running it?

Well, all we need to do is make sure our Docker for Windows is in Linux container mode (right-click the Docker icon in the system tray and say “Switch to Linux containers”) and then in the same folder as our docker-compose.yml file, enter the command:

docker-compose up

What will happen next is that the “wordpress” and “mariadb” Docker images will need to be downloaded first time which might take a minute or two, but once you have them, the two containers will start up, and before you know it you’ll have a fully working WordPress system.

Just navigate to http://localhost:8080 and you’ll be ready to start customising your brand new WordPress installation.

Pretty straightforward. Compared to the hours of pain I used to go through whenever I tried to manually install WordPress on a VM, this is ridiculously easy. If I want to experiment with a new WordPress theme or plugin, I can do it in minutes in a sandboxed environment I can throw away if necessary.

When I’m done I can tear down the whole thing with docker-compose down, or just stop it temporarily with docker-compose stop.

Low-Level Container Access

Now, you might be wondering, what happens if I need to get direct access to these containers and make my own customizations? For example, I wanted to import the contents of another WordPress site into my container, but couldn’t do so because the PHP upload_max_filesize setting wasn’t set to a large enough value.

Well the great thing is that you can very easily get access inside your containers with docker exec. I used docker ps to find the identifier of my WordPress container, then docker exec -it b12a bash got me into a bash prompt inside the container.

From there I was able to install a Linux text editor I can actually save and exit with (unlike vi which I seem incapable of learning), and edit my .htaccess file:

apt-get update
apt-get install nano
nano /var/www/.htaccess

And that’s just one example of the power of docker exec. We could also connect to the MySQL container to query the database. We can even customise our docker-compose.yml file to mount local folders into our containers. So as well as having a really quick setup experience, we still have the power to be able to dive in at a low level if we want.

The next step is of course to host this WordPress Docker installation in the cloud. But that’s a subject for a future blog-post.

About Mark Heath

I'm a Microsoft MVP and software developer based in Southampton, England, currently working as a Software Architect for NICE Systems. I create courses for Pluralsight and am the author of several open source libraries. I currently specialize in architecting Azure based systems and audio programming. You can find me on: