Using Docker compose to develop plugins – Part 1

0
(0)

When I first started the Develop with WordPress site, some of the earliest articles I wrote were a series on installing wordpress. These articles are still a useful resource when it comes to installing on a server or laptop running Linux, but often you don’t want to install server processes on a laptop and so this article is going to start to outline another solution in those situations. This article is the first in a series which will describe how to set up a development environment using docker compose to develop WordPress plugins.

I should point out at this point that the docker compose system I’m going to show in this series is the result of taking several bits of various people’s docker systems and bolting it together in a system which works for me. When I first started using docker I tried quite a few individual setups but I couldn’t get them to work in the way I wanted so I adapted bits of all of them to get my system working.

What is Docker Compose?

Docker is a container based, virtualisation system which allows you to run individual applications in their own separate Linux environment [efn_note]Actually, containers don’t have to run in a Linux environment, but the vast majority do[/efn_note]. Each application runs in complete isolation but can be connected to other container applications, or applications running on the host machine, via network ports and files.

Using Docker, you can start up a mysql database server, a web server, a Redis cache etc and wire them all up to provide the system you want, and it can all be started and stopped with a series of commands which create the individual containers. As well as being convenient because of the speed with which the system can be up and running, it is also a repeatable setup. Once configured, the environment is exactly the same irrespective of whether it’s started on your linux server or your mac or windows laptop.

Docker compose is a system which builds on docker to provide a single file which describes the system you want and start it all up with one simple command. With a docker compose system you can describe all the services you need and how they communicate, and with a single start command you have a full development WordPress installation running in a few seconds.

Setting up Docker Compose

In order to set up the development environment you need to have Docker and Docker compose installed on the host machine. If you follow the links to Docker above you can find install instructions for Docker and Docker Compose on all host operating systems.

Once the docker applications are installed, create a new directory to put the docker compose file and plugin development directories in.

mkdir docker
cd docker
mkdir db www plugin build files
touch build/Dockerfile
touch docker-compose.yml

The last two commands create a docker build file and a docker compose file which, as the extension probably tells you, is a yaml file. I’m going to describe the contents of these files in the next section.

Dockerfile and docker compose file

The contents of the Dockerfile is:

FROM wordpress:5.0.3-php7.2-apache
# Update the base image and install some utility programs
RUN apt-get update && apt-get install -y sudo less mysql-client && rm -rf /var/lib/apt/lists/*

# Install xdebug to enable debugging
RUN yes | pecl install xdebug 
    && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini 
    && echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini 
    && echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini

# Install the WP-cli program so we can automate the wordpress install
RUN curl -o /bin/wp-cli.phar https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

# Copy some programs into the container
COPY ./bin/wp-su.sh /bin/wp
RUN chmod +x /bin/wp
RUN chmod +x /bin/wp-cli.phar
RUN mkdir /var/www/.wp-cli && chown www-data:www-data /var/www/.wp-cli
RUN chown www-data:www-data /var/www/html -R

RUN mkdir /var/www/plugins && chown www-data:www-data /var/www/plugins
# Copy some plugins which are in zip format
COPY ./plugin/*.zip /var/www/plugins/

# Copy files which have content.
RUN mkdir /var/www/import && chown www-data:www-data /var/www/import
COPY ./site-content.wordpress.xml /var/www/import
COPY ./site-menu.xml /var/www/import

This file is used by docker to build the virtualised machine which is going to act as the webserver for the wordpress site. Some things to note here are:

  • The base operating system of the container is a debian based system which is pre configured to have wordpress 5.0.3, php 7.2 and apache.
  • In the fie, I’ve added comands to install xdebug to enable debugging once the container is running
  • I have some plugins which I install on most sites for which I have a licence. It is necessary to install these from a zip archive file so I copy them into the container to do that
  • In this instance I’m creating a development version of an existing site, so I have some content to initialise the system with. If you don’t want to do that you can remove the relevant lines.

The docker-compose.yml file looks like this:

version: "3"

services:
  mywordpress:
    build:
      context: .
      dockerfile: build/Dockerfile
    image: mywordpress:latest
    environment:
      VIRTUAL_HOST: "wordpress.localhost"
      WORDPRESS_DB_HOST: "mysql"
      WORDPRESS_DB_NAME: "wpdb"
      WORDPRESS_DB_PASSWORD: "secret"
      WORDPRESS_DB_USER: "root"
      XDEBUG_CONFIG: "remote_host=192.168.68.149"
    depends_on:
      - "mysql"
    networks:
      - "webnet"
      - "dbnet"
    volumes:
      - "./www:/var/www/html"
      - "./plugin/myplugin:/var/www/html/wp-content/plugins/myplugin"
      - "./files/install-wordpress.sh:/usr/local/bin/install-wordpress:ro"
      - "./files/install-plugins.sh:/usr/local/bin/install-plugins:ro"
      - "./files/import-wordpress.sh:/usr/local/bin/import-wordpress:ro"

  mysql:
    image: "mariadb:10.2"
    user: "1000"
    volumes:
      - "./db:/var/lib/mysql"
    environment:
      MYSQL_DATABASE: "wpdb"
      MYSQL_ROOT_PASSWORD: "secret"
    networks:
      - "dbnet"
  
  proxy:
    image: "jwilder/nginx-proxy:alpine"
    ports:
      - "80:80"
    networks:
      webnet:
        aliases:
          - "wordpress.localhost"
    volumes:
      - "/var/run/docker.sock:/tmp/docker.sock:ro"

networks:
  webnet: {}
  dbnet: {}

The docker-compose.yml file is doing the following:

  • It’s setting up three individual containers which are going to act as the database, the wordpress host with apache webserver and a proxy host. The proxy host is not strictly required, but I use it because I sometimes have more than one containerised service running on port 80 and the proxy allows it.
  • The web server and the database are set to store information in the directories called db and www in the directory structure we set up. This isn’t required – we could have the volumes created by docker and let it handle everything, but I quite like to have the data visible in the file system in case I want to debug.
  • Some bits of information would need to be changed for your particular setup. For example in the mysql section of the docker-compose.yml file there is a reference to an id. This is set to 1000 which is my user id on my laptop. You would need to change that to your id which is found by running id -u. Also, the domain name is set to wordpress.localhost in a couple of places, and that can also be changed.

That covers the basic settings for the docker containers. In the next part of this series, I’ll cover the other files which are added to the directory and the commands used to get the containers running and automate the wordpress install, plugin install and data initialisation.


How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Please consider sharing on social media!

Leave a Reply

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

Bosch EasyControl Previous post Bosch EasyControl room stat and smart rad controllers
Next post Docker compose to develop plugins – Part 2