Development

WordPress Development with Docker containers (MacOSX)

June 22, 2016

June 22, 2016 by Mark Donkers

As new developers join our team, we make it easy and straight-forward to set up local wordpress development environments that closely resemble the test/staging and production environments.

Why should this be considered best-practice? When developing for a multi-site wordpress website in a corporate environment, most code will need to be written locally on your desktop machine as there are processes in place that prevent a developer having ftp access to upload his/her work to quickly test. When dealing with build and deployment processes it is vital to have a local environment where everything functions as it would on the server based environment.

By utilising Docker we are able to clone our server environment for local development machines. With the use of Github, we control the platform locally and map local git controlled folders into locations within the docker container. This enables us to work with remote repositories, while maintaining the exact same build model. As a result we can work locally, test code locally and only commit work to the test/staging environment once it’s running smoothly on the local machine.

This article will explore how to set up this Hybrid/Docker environment suitable for managing a multi-site wordpress setup. Although a new method is already in the works with a scripted compose file (DockerFile), this option demonstrates how to work with Docker and Shell scripts to set up your environment.

As few steps are needed and some handy shell scripts (Linux, MacOS) have been included in this document, this article will mainly focus on the setup and installation for a Mac workstation. This guide assumes you have a working wordpress installation deployed into a git based repository.

Note: Some steps can be arranged from finder, as most of the others have to be handled through the terminal. Most steps described are designed for Ebury’s wordpress multi-site setup but, with minor adjustments, you can easily adapt it to your own needs. You would need 2 docker images, a LAP stack (Linux/Apache/PHP) and a separate Mysql Docker image, you may also need to update the env_up.sh file to point to the newly selected images. You can always drop me an email if you’re stuck and need some assistance!

Files referenced in this article can be found on Ebury’s Open Source Git Repository.

Step-by-step guide

Getting prepared

  1. Download and install Docker Toolbox https://www.docker.com/products/docker-toolbox
  2. Download and install SourceTree https://www.sourcetreeapp.com/(or any other Git based tool you’re familiar with)
  3. Download and install your favourite Code Editor (e.g. Sublime)

Setting up your local file system

  1. Create a folder in which you would like to keep your checked-out repositories (e.g. /Users/{username}/myDevelopment/)
  2. Create a folder where you would like to store the shell scripts to manage the environment (e.g. /Users/{username}/myTools/)
  3. Take note of the folder you created in step [2.1] as you will be needing it again
  4. Check out your repository (-branch) in the folder you created in step [2.1]
  5. Copy the files (env_up.sh / env_down.sh / hostswitch.sh ) attached below in the ‘Files / scripts‘ section into the folder you created in step [2.2]
  6. Add execute rights to the 3 files copied in step [2.5] (e.g. chmod +x env_up.sh)
    1. RPLOCATION = the location in step [2.1] + root folder of your project
    2. DBVERSION = the database container version number
    3. VMVERSION = the VM (webserver) containerOpen the env_up.sh file and set the variables to the value which resembles your setup and versions.
  7. Create three new files; hosts.defaulthosts.test and hosts.staging (as root) in /etc and copy the contents of the host’s file replacements link into each file respectively.
  8. One default docker will use IP address 192.168.99.100. If your installation is using a different IP address, then you need to replace all occurrences of 192.168.99.100 with your IP address.
  9. To find out which IP address the docker VM is using, open the docker CLI (aka Docker Terminal), either by directly opening the terminal or via Kitematic GUI (Note: Depending on the state of the Docker environment, the terminal may already show the IP when opened. That would remove the need for performing step [2.11])
  10. At the prompt enter:
    docker-machine ip

Getting up and running

  1. Set up an docker account to obtain your docker images
  2. Login to the docker hub from the command-line:
    docker login --username=yourhubusername --email=you@emailprovider.com
  3. Open the terminal, and go into the folder you created in step [2.2]. Then, once the cursor returns enter:
    ./env_up.sh

    This step can take some time, depending your internet connection, since it will need to download the images from the Docker Hub repository to your computer. In the current state it will not provide a progress bar or estimated duration, so you will need to be patient and wait for the prompt to return. Depending on the images, the first download might be 1Gb or more.

  4. Update your computer’s host file to reflect the environment you want to simulate (e.g. test). At the prompt enter:
    sudo ./hostswitch.sh test

    Note: This will update your hosts file to test domains pointing to your local environment.

Test and go

  1. All should be running and you should be able to open a browser and point it to one of the domains.

Start the environment after the host restarts

  1. Start the docker terminal as explained in [2.10]
  2. Once the cursor returns in the console:

    cd {folder created in step 2.2}
    ./env_up.sh

In this way you can set up local development environments that mirror test/staging and production.

Good luck!

Share on Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

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