Docker compose to develop plugins – Part 2
In this, the second part of the series showing a docker compose based development environment for wordpress, we look the rest of the files in the system and the commands which are run to complete the install and configuration of the site. A reminder that the first part of the series, which covered the Docker file and docker compose file is available here.
Additional files
The additional files which are referenced in the Dockerfile and the docker compose file, are used to run the site install, the plugin install and if applicable the site import. There is also a wrapper script which makes the install commands run as user www-data rather than as root[efn_note]If the scripts ran as root they would mess up the permissions and the site wouldn’t work properly[/efn_note].
The first script is the simple wrapper script wp-sh.sh.
!/bin/sh # Wrapper script which allows wp-cli to run as www-data sudo -u www-data /bin/wp-cli.phar "$@"
This is fairly easy to understand – it just takes any parameters passed to it and wraps it round a sudo -u www-data
so the command runs as the apache user.
The install-wordpress.sh command runs the famous wordpress 5 minute install and looks like this:
#!/usr/bin/env sh chown www-data:www-data /var/www/html -R # Install Found Film Development WordPress. wp core install --title="Wordpress Development site" --admin_user=admin --admin_password="a-secret-admin-password" --admin_email="[email protected]" --url=http://wordpress.localhost --skip-email chown www-data:www-data /var/www/html -R # Activate the plugin you are developing. wp plugin activate myplugin
Again it’s pretty obvious what is happening here but this file will need to be changed so that it has the settings you want for your site.
Finally the install plugins command file install-plugins.sh has this basic content:
#!/usr/bin/env bash # Make sure the /var/www/html directory is owned by www-data chown www-data:www-data /var/www/html -R # Install plugins used on your site # Either from the net wp plugin install jetpack --activate # Or from a file wp plugin install /var/www/plugins/classic-editor.1.4.zip --activate
This file will need to be altered to add the individual plugins your site needs. I’ve just put some example plugins which I tend to use for my projects, but you can put any you need instead.
There is one other optional step which is covered by a script called import-wordpress.sh. You would use this if you are setting up a development version of an existing site and want to import some content. In that case you would export the site as an xml file and place it in the files directory with the name site-content.wordpress.xml. You could also create an export of the menu structure you use and place that in a file called site-menu.xml. If you have those files then this is what goes in the import-wordpress.sh script:
#!/usr/bin/env sh wp import --authors=create /var/www/import/site-content.wordpress.xml wp import --authors=create /var/www/import/site-menu.xml
Final Setup
With these files in place, and the plugin you are developing installed in the plugin/myplugin directory there are a few commands we need to run to get the site up.
First, so make sure the permissions are correct we do the following:
chmod +x files/*.sh chown simon:simon db -R chown simon:simon www -R
This will make sure that the shell scripts which do the wordpress etc install are executable and also that the db and www directories are writable. Obviously you would change the user name to be your login name.
With this set you can run:
docker-compose up -d
and assuming everything is ok you should see
Creating network "docker_dbnet" with the default driver Creating network "docker_webnet" with the default driver Building mywordpress Step 1/12 : FROM wordpress:5.0.3-php7.2-apache ---> 7539ce0f28d0 Step 2/12 : RUN apt-get update && apt-get install -y sudo less mysql-client && rm -rf /var/lib/apt/lists/* ---> Using cache ---> ac58cdccfb58 Step 3/12 : 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 ---> Using cache ---> 7f555e77c6b4 Step 4/12 : RUN curl -o /bin/wp-cli.phar https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar ---> Using cache ---> 682cbf20a78f Step 5/12 : COPY ./files/wp-su.sh /bin/wp ---> Using cache ---> 6dc5fae751ed Step 6/12 : RUN chmod +x /bin/wp ---> Using cache ---> 4e28d604a763 Step 7/12 : RUN chmod +x /bin/wp-cli.phar ---> Using cache ---> c71a7cdef0fb Step 8/12 : RUN mkdir /var/www/.wp-cli && chown www-data:www-data /var/www/.wp-cli ---> Using cache ---> 036624c98931 Step 9/12 : RUN chown www-data:www-data /var/www/html -R ---> Using cache ---> 618a5f76a9e3 Step 10/12 : RUN mkdir /var/www/plugins && chown www-data:www-data /var/www/plugins ---> Using cache ---> 1159d248460d Step 11/12 : COPY ./files/*.zip /var/www/plugins/ ---> Using cache ---> 817b0ab747a3 Step 12/12 : RUN mkdir /var/www/import && chown www-data:www-data /var/www/import ---> Using cache ---> a561827cf1f7 Successfully built a561827cf1f7 Successfully tagged mywordpress:latest Creating docker_mysql_1 ... done Creating docker_proxy_1 ... done Creating docker_mywordpress_1 ... done
If you don’t get this, there will be an error message which should show what went wrong.
Once the containers are running the wordpress site is available but there are still some steps to do. First you need to add a local hosts entry which maps the domain you have called your development site (in this case wordpress.localhost) to the local machine ip. This is normally as simple as adding 127.0.0.1 wordpress.localhost into your local hosts file.
Then, to complete the install process run
docker-compose run --rm mywordpress install-wordpress docker-compose run --rm mywordpress install-plugins # And Optionally docker-compose run --rm mywordpress import-wordpress
Each of these will carry out their tasks to set up the site, install the plugins and if applicable import some content. Once these sites are complete the new wordpress site will be available at http://wordpress.localhost. You can login to it with the user and password you set in the install-wordpress.sh script.
In the final part of this series, I’ll go through setting up debugging so that you can really start plugin development.