Docker Compose for WordPress

Local development for Wordpress

Posted by Harald Nezbeda on Thu 15 September 2016

As mentioned in Development environments for Developers, I consider Docker a valuable resource for creating and shipping environments between developers. But using additional tools, such as Docker Compose, you can step this even further into creating an entire tree of components that can be simply shared and massively increase the speed of setting up a development environment.

Lately I published on Github a package for WordPress, which is called WordPress Docker Compose or short WPDC. I added all that is required in the file, but i decided to go on and describe some concrete use cases in the following lines. Additionally I want to mention that this package is for straight WordPress development, but if you have some docker knowledge you can go ahead and add additional services into the docker-compose.yml such as Solr, Elasticsearch, Redis or whatever you require into your project.

Create a new project

The main idea of this is to skip the boring part of creating a server, creating a database, setting a vhost and install WordPress. This is a redundant process which is just wasting time as you have to do the same thing over and over again without adding any value. This is why automating this process with a tool such as Docker may be pretty handy.

All you need is to copy the docker-compose.yml file into a local folder. In the file you may change the IP address of the WordPress service and the db service, as you will use this to access the WordPress instance into your browser and the database (you may use a tool like HediSQL for this). Make sure you have the latest versions of Docker and Docker Compose installed on your machine. All you have to do now is to run docker-compose up in your terminal from the folder you have the docker compose file. You may need root access.

This create 2 new folders.
  • wp-data - used to store and restore database dumps
  • wp-app - the location of your WordPress application

The containers are now build and running, from now you can use the start command to run the containers (“docker-compose start”) and if you finish your work you can use the stop command (“docker-compose stop”). You should be able to access the WordPress installation with the configured IP in the browser address. For convenience you may add a new entry into your hosts file.

Start from existing source

Copy the docker-compose.yml file into a new directory. In the directory you create two folders:

  • wp-data - here you add the database dump
  • wp-app - here you copy your existing WordPress code

You can now use the up command: docker-compose up

This will create the containers and populate the database with the given dump. You may set your host entry and change it in the database, or you simply overwrite it in the wp-config.php by adding:

  • define('WP_HOME','http://wp-app.local');
  • define('WP_SITEURL','http://wp-app.local');