Running WordPress using Docker

You want to run your own instance of WordPress? Why not use Docker to containerise the deployment to make maintenance and potential host switching easy?

I have set this up – as mentioned in my first post – to run this blog.

Prerequisites

First thing you need to do is: install Docker. You will need the “community edition” (CE) to follow the examples in this post. I am not going to cover this installation as it heavily depends on your host operating system. But as it is easy to do you will figure it out following the steps on the docker website. In addition you will need to install docker-compose because it simplifies the process of defining and running docker container a lot. To make working with different containers easier I strongly recommend to install the bash command completion.

You do not need a server in the cloud to follow this post. In fact I recommend to configure and test on your local machine first. After everything works you can simply deploy to any Docker host in the cloud.

I have used a Mac book with Docker to configure and test the installation. On my cloud host I run Debian 9 (stretch) as operation system with Docker 17.12.0 and docker-compose 1.18.0 installed. All examples in this post are based on this setup.

Which components do you need?

For a standard WordPress installation you need three components.

  1. Web server: we will use NGINX as it scales incredibly far with limited resources. Since the NGINX server is single-threaded and processes are not spawned to handle each new connection, the memory and CPU usage tends to stay relatively consistent, even at times of heavy load.
  2. WordPress: we will use WordPress with PHP-FPM as it runs as a standalone FastCGI server and NGINX connects to the server directly. The configuration is more complex but the good news is: you do not have to deal with configuration issues as we will use a preconfigured Docker container image. You find more information about the benefits of using PHP-FPM here: https://www.cloudways.com/blog/php-fpm-on-cloud/.
  3. Data base: we will use MySQL (MariaDB); just because. 😀

How to set WordPress up?

Are you already downloading some of the archives for WordPress, NGINX and MariaDB? STOP! Because we are going to use Docker there is no need to download or install anything manually.

To make configuring docker simpler we are going to use docker-compose. To tell docker-compose which services to install and how to configure these we need to create a compose file (docker-compose.yml).

Your want to make your life easy? Pull my GitHub repository with these examples here:  https://github.com/torstenknoefel/DWP-examples.git.

Let’s start with MariaDB

Open your editor of choice – I use nano – and create the following docker-compose.yml file:
(If you have pulled my git repository  cd DWP-examples/mariadb and have a look at docker-compose.yml.)

Save the file, create a file “.env”. This contains environment variables and is automatically sourced by docker-compose:

Create a directory “db-data” which will hold the db data (funny that) and type docker-compose up  into your shell. You will see the following output:

Terminal session: creating the MariaDB container

Docker downloads the necessary components for the MariaDB image, builds the image and starts the MariaDB container.

Now you have the MariaDB up and running, listening to port 3306.

Press CTRL-c  to stop the container running.

Add the WordPress container

Open the Compose file again and add the following marked lines:
( In case you want to piggy back on my GitHub repository:   cd ../wordpress.)

Save the file. Create a directory “wordpress” where WordPress will store its php files.

To enable uploads bigger than 2 Megabyte – most WordPress themes are bigger – you need to create the following “uploads.ini” file as the default php installation will limit file uploads to 2 Megabyte. You also need to tell NGINX about the file upload limit – I will come back to this later when configuring the NGINX container.

Type docker-compose up  into your shell. You will see the following output:

Terminal session: starting the wordpress container

You will notice that docker starts MariaDB first and then WordPress-FPM. As both run truly non-synchronised in parallel you might see some race conditions during startup which will resolve themselves.

Looking into the directories “wordpress” and “db-data” you see the persistent files for the WordPress installation and the database. These have survived although you stopped the container.

Finish the setup with the NGINX container

Open the Compose file again (the last time I promise) and add the following marked lines:
( Using my GitHub repository:   cd ../nginx.)

Save the file. Create a directory “logs/nginx” where NGINX will store its log files.

Create a directory “nginx” and a file “nginx/wordpress.conf”:

For detailed information about configuring NGINX please see: https://www.nginx.com/resources/admin-guide/

Add the environment variable “WEB_PORT” to the .env file:

Type docker-compose up   into your shell. You will see the following output:

Terminal sessions: starting MariaDB, WordPress and NGINX

Wait for WordPress to become ready saying: “NOTICE: ready to handle connections”, start your browser of choice and open http://localhost:8080/:

Initial WordPress install page

Voila, WordPress is up and running. Press CTRL-c  to stop the container running.

To clean up use  docker-compose down  this will remove all container.

Get WordPress ready for production

You now have WordPress running on your local machine, nice eh? But pretty useless.
To get the installation ready for production you need to do the following:

  1. Get yourself a Docker installation in the cloud. You find my setup here.
  2. Login as a docker enabled user.
  3. Clone my repository.
  4. Copy the “nginx” tree into a directory (e.g. “my_wordpress”) of your choice and change to that directory.
  5. Change “WEP_PORT” in the .env file to 80 – you want a “nice” URL not one with “:8080” at the end…
  6. Set “MYSQL_ROOT_PASSWORD” in the .env file to a password of your choice.
  7. Change the “server_name” in  nginx/wordpress.conf to the domain pattern you want NGINX to serve.
  8. Start the installation with  docker-compose up -d. The “-d” option stand for detach thus your installation runs in the background.

You have done it. Congrats. Start blogging and let me know about your blog.

 

In the next post I show you how to deploy to Docker remotely over TLS.

Stay tuned.

 

Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “Running WordPress using Docker”