Posted by & filed under PHP, Web development.

WordPress is used to make about 43.7 million posts each month1. With an extensive library of over 33,500 plugins, WordPress has a significant market share of both the content generation and development communities. Creating plugins for WordPress is not overly difficult, but the actual development and testing process can be a hassle.

Let’s see how Docker can help us out.

Prerequisites:

Docker (boot2docker on Mac OS X)

Running WordPress inside Docker

We’ll start by pulling a WordPress image.

> docker pull tutum/wordpress

(boot2docker users only) Grab the IP address of your virtual machine by running boot2docker ip.

Next, we need to make some changes to the container that will let us easily see any PHP errors. Jump into the container by overriding the default command with /bin/bash and adding the -it flags to run.

> docker run -it --name="wp" -p 8080:80 tutum/wordpress /bin/bash

Once in the container, we need to modify the following files:

/etc/apache2/apache2.conf Replace ErrorLog ${APACHE_LOG_DIR}/error.log with ErrorLog /dev/stderr
/etc/apache2/sites-enabled/000-default.conf Replace ErrorLog ${APACHE_LOG_DIR}/error.log with ErrorLog /dev/stderr
/etc/supervisor/conf.d/supervisord-apache2.conf Add redirect_stderr=true after autorestart=true
/etc/supervisor/supervisord.conf Add loglevel=debug after childlogdir=/var/log/supervisor

 

Exit the container and commit the changes we made to a new image.

> exit
### Back in terminal ###
> docker commit wp wordpress-debug
# Remove the temporary container we created
> docker rm wp

Now, run the new image we just created:

> docker run --name="wordpress-test" -p 8080:80 wordpress-debug /run.sh

Open your web browser and visit http://[IP FROM BOOT2DOCKER IP COMMAND]:8080. If all went well, you should be prompted to begin installing WordPress. Continue through the installation process and then login to the WordPress admin.

After developing your plugin (actual plugin development is not covered in this article), visit Plugins -> Add New and upload your zipped up plugin. Test the plugin and watch the Docker output for errors.

Once finished, Control+C will terminate the Docker container.

Warning: Deleting the Docker container will remove all content from the container.

To start with a fresh WordPress installation or if plugin errors break WordPress, simply delete the container and start a new one:

# Remove the container
> docker rm wordpress-test
# Start a new container
> docker run --name="wordpress-test" -p 8080:80 wordpress-debug /run.sh

Enjoy quick and pain-free temporary WordPress installations that are suited for plugin development.

Cheers!

 

Sign up for Turret.IO — the only data-driven marketing platform made specifically for developers.

1 http://en.wordpress.com/stats/

Leave a Reply

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