Super Easy Dockerised WordPress

Super Easy Dockerised WordPress

After finding that the official WordPress Docker container is broken (PHP errors here and there in the dashboard) I decided to create my own and to share it with you, along with instructions on how to containerise your data so it persists.

In this simple, step-by-step walk-through we will create the following 4 Docker containers:

  • One for your WordPress database so it persists across container instances (unless you delete it of course!)
  • A MySQL server
  • A data container for your WordPress content (uploads, themese, WordPress installation)
  • An Apache2 server with PHP5, runnning Ubuntu for hosting the WordPress site

Step By Step

There are just three, possibly four variables you need to set, the rest is copy-paste :-)

  • $SITE_NAME$ - A Docker-friendly (no spaces, no capital letters allowed!) name for the bundle of 4 containers we’ll be creating for your site
  • $MYSQL_PASSWORD$ - Choose a good MySQL password
  • $HTTP_PORT$ - The port you want your site to be accessible at, usually 80 is a good port number for a web server.
  • $WORDPRESS_DB_EXPORT_FILENAME$ - Only needed if importing data from an existing WordPress installation. You’ll first need to do a mysqldump of your WordPress database

MySQL Containers

We will create two containers for MySQL. One for storing the actual database and another for the MySQL server. Keeping the data in it’s own container means it should be persisted when changes are made to the server container.

First grab the latest MySQL container

docker pull mysql

After that’s downloaded let’s go ahead and create the portable data container

docker create --name $SITE_NAME$-mysql-data -v /var/lib/mysql mysql

We can then fire up the MySQL server using this

docker run --name $SITE_NAME$-mysql --volumes-from $SITE_NAME$-mysql-data -e MYSQL_ROOT_PASSWORD=$MYSQL_PASSWORD$ -d mysql

Now it’s time to set up the database. We have a couple options here:

  • We can either start a fresh WordPress installation
  • or import the database from an existing WordPress blog

Fresh WordPress Installation

To set up a fresh WordPress database run a new MySQL container linking to the MySQL server container we created earlier. This will give us access to the MySQL CLI which we can use to connect to the MySQL server via it’s hostname mysql

docker run -it --rm --link $SITE_NAME$-mysql:mysql mysql /bin/bash

From the bash prompt run these in

root@0d178a28284a:/# mysql --host=mysql --user=root --password=$MYSQL_PASSWORD$
mysql> create database wordpress;
mysql> exit
root@0d178a28284a:/# exit

Import Existing WordPress Database

As with setting up a fresh WordPress installtion we need to get access to the MySQL server container we created earlier:

docker run -it --rm --link $SITE_NAME$-mysql:mysql mysql /bin/bash

Then import your exported MySQL data:

root@0d178a28284a:/# mysql --host=mysql --user=root --password=$MYSQL_PASSWORD$ wordpress < $WORDPRESS_DB_EXPORT_FILENAME$

WordPress Containers

As with the MySQL containers we will create two WordPress containers. One for the data and another for the Apache2 server that will host the WordPress site

Let’s make the data container

docker create –name $SITE_NAME$-wordpress-data -v /var/www/html ubuntu

Now to set up the WordPress installation and content

docker run -it –rm –volumes-from $SITE_NAME$-wordpress-data ubuntu /bin/bash

If this is a new WordPress installation you should run the following commands now you’re inside this temporary container to get and extract the latest WordPress and place it in your persistent data container at /var/www/html

root@02a433fc21b4:/# apt-get install -y wget root@02a433fc21b4:/# cd /var/www/html root@02a433fc21b4:/# wget https://wordpress.org/latest.tar.gz root@02a433fc21b4:/# tar -xzf latest.tar.gz root@02a433fc21b4:/# mv wordpress/* . root@02a433fc21b4:/# rm -rf wordpress latest.tar.gz root@02a433fc21b4:/# chown -R www-data:www-data /var/www/html root@02a433fc21b4:/# exit

If you are migrating an existing WordPress site into Docker you can import that data into the /var/www/html folder which will be persisted once you exit. Everything else will be lost when we exit from the container because of the –rm flag. This means we can install whatever utilities we need to get the data.

Finally let’s run the WordPress server container:

docker run –name $CONTAINER_NAME$-wordpress –volumes-from $CONTAINER_NAME$-wordpress-data –link $CONTAINER_NAME$-mysql:mysql -p $HTTP_PORT$:80 -d antfie/wordpress

And we’re done. Let’s just check that everything’s all good using

docker ps -a

Which should return something like this:

alt text

Go browse http://localhost:$HTTP_PORT$ and you should be greeted with WordPress.

Any questions or problems let me know.

comments powered by Disqus

Contact

Thanks for getting in touch.

Anthony should reply within 24 hours.

Oops, there has been a problem.

We couldn't process that request. Please try again.

Get in touch using the form below or via @antfie.