Call + (44) 0843 289 4539

Using Docker for WordPress Development

We do a lot of custom WordPress development and are constantly searching for new ways to evolve and improve our WordPress development. In the past, we developed what we called the Poor Mans Mamp, which worked fine and it help for a while. However, it wasn’t perfect and we found it did introduce a little bit of friction into our workflow.

We kept exploring and the result of this has been the introduction of Docker, which may actually be the solution we’ve been searching for.

In this post we’ll guide you through our process of using Docker for WordPress Development

Code available on Github

As software developers and Digital marketers, we will be working on a number of different projects at any one time. Each project will typically have its own unique configuration settings.   We might be working on a project for a customer who has actually tailored to a version of WordPress, and the result may be that they have it locked down to a particular version of WordPress.

In another example, the WordPress element of the website may actually be integrated with Magento or another eCommerce engine.

Problems incurred by many different instances WordPress

Your WordPress project may also be restricted to a particular version of PHP, Linux Operating system or even an obscure combination of both with a different web server configuration i.e Nginx or Apache.

To configure and manage several local development environments on your laptop and also enable easy switching between each environment can be difficult and time-consuming.

We have previously written a number of blog posts providing instructions on how we attempted to configure and manage these local WordPress development environments in the past:

Sure these solutions work, but they were always finicky and invariably when you’re constantly moving between environments, and incurred friction continuously having to configure environments.

Docker to the rescue

Docker removes a lot of this hassle and now makes it incredibly easy to get your environments configured usually taken only a few seconds. Saving a lot of time and frustration.

Get started with Docker tutorials

Docker runs on all popular operating systems so be sure to follow the instructions on the docker website on how to install it on your system.

How to install Docker on Ubuntu for Developers

Install Docker compose

If your Docker application includes more than one container – as is the case for WordPress ( web server and database running in separate containers) – building, running, and connecting the containers from separate Dockerfiles is cumbersome and time-consuming. Docker Compose solves this problem by allowing you to use a YAML file to define multi-container apps.

You can configure as many containers as you want, how they should be built and connected, and where data should be stored. When the YAML file is complete, you can run a single command to build, run, and configure all of the containers.

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

There are detailed instructions to on the docker compose website to ensure you have the latest version installed.

Install WordPress Via Docker Compose

Once you have Docker and docker-compose all setup, moving on to the next phase is really simple.

Quickstart: Compose and WordPress

These instructions are great if you just want to get a version of WordPress up and running on your machine. However, if you want to build a complete development environment you will need to extend the docker-compose.yml



Install phpMyAdmin with docker-compose

We’ll enhance the standard WordPress docker-compose.yml by adding a phpmyadmin to help with basic database administration tasks.

We’ll grad the official phpMyAdmin image. link it to our wordpress_db container with the name mysql, expose its port 80 on port 8080 of the host system, and finally sets a couple of environment variables with our mySQL username and password.

 

  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - 8080:80
    environment:
      PMA_HOST: db
      MYSQL_ROOT_PASSWORD: password12@
    networks:
      - back

Map to a local file system

It is possible to store the document root for WordPress on the host filesystem using a Docker data volume to share files between the host and the container.

We will also map the WordPress file system to our local drive, in order to be able to carry both theme and plugin development.

The WordPress Docker container is configured to check if the root HTML  is empty or not when it starts and copies files there appropriately. i.e. /var/www/html

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    restart: always
    volumes:
      - ./wp-content:/var/www/html/wp-content 
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_PASSWORD: password12@
    ports:
      - 80:80 # Expose http and https
      - 443:443
    networks:
      - back



Using docker-compose to launch WordPress

To use the Docker-compose file, using the terminal window simply change directory to the folder that contains the docker-compose.yaml and use the command docker-compose up -d.

Once it’s complete, you should be able to open a browser to browse http://localhost  and http://localhost:8080/phpmyadmin.

 

Other useful docker-compose commands:

# Starts all stopped containers in the work directory
docker-compose start
# Stops all currently running containers in the work directory
docker-compose stop
# Validates and shows the configuration
docker-compose config
# Lists all running containers in the work directory
docker-compose ps
# Stops and removes all containers in the work directory
docker-compose down

 

Develop themes and plugins

It is possible to develop specific components for WordPress, like themes and plugins. All it needs is a proper configuration of Volumes in the docker-compose.yml file.

theme development

volumes:
- ./theme-name/trunk/:/var/www/html/wp-content/themes/theme-name

plugin development

volumes:
- ./plugin-name/trunk/:/var/www/html/wp-content/plugins/plugin-name

Summary

You should have a full WordPress deploy up and running.

You should be able to use the same method to create different version installations of WordPress that you can use for testing purposes, just changing the WordPress version.

running Docker commands to create containers already simplifies application management, docker-compose takes it a step further and allows you to bundle multiple containers within a single working directory.

Follow Me

Gary Woodfine

Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.
Follow Me
%d bloggers like this: