Setting up a local WordPress development environment with Docker on Ubuntu 17.04

After working on a fair amount of sites recently, I’ve been looking for a better way to streamline the process for setting up a local development environment for WordPress sites that I work on. While I’m sure I’m not at the end of this journey, I’ve finally made the plunge into the wonderful world of Docker and it’s been great thus far.

 

This post essentially includes information from some other resources I’ve found online, mixed with some modifications from my end to make it better for my set-up. It also assumes you’re using a fresh installation of Ubuntu 17.04, but “freshness” doesn’t necessarily matter. YMMV.

 

 

To start, let’s install the tools we need for setting this up:

This will update your package lists and install the docker and docker-compose packages on your system.

sudo apt-get update && sudo apt-get install docker docker-compose -y

Once that’s all set, we need to run a quick command to make sure that we can run everything as your non-root user and also that we’ll have access to files later on:

usermod -aG docker,www-data ${USER}

At this point, log out and back in as your same user and you should be good to go, confirm so with:

groups ${USER}

And you should see docker and www-data listed. With that all set, let’s create our directory we’ll be handling everything else in:

mkdir wordpress_dev && cd wordpress_dev

We’re officially in our new working directory, so let’s get to work. First, create a new file called “docker-compose.yml”.

touch docker-compose.yml

This is going to be the configuration file that we’ll use to define how our containers should be set up. At this point, you can either run the following:

cat >docker-compose.yml <<EOL
wordpress_app:
image: wordpress
links:
- wordpress_db:mysql
ports:
- 8080:80
volumes: 
- ~/wordpress_dev/wp_html:/var/www/html
wordpress_db:
image: mariadb
environment:
MYSQL_ROOT_PASSWORD: your_password_here
volumes:
- ~/wordpress_dev/wp_db:/var/lib/mysql
EOL

To generate the file (make sure to change the “your_password_here” value to an actual password string), or you can instead open the file with your preferred text editor┬ávim and just copy paste the following into it:

wordpress_app:
  image: wordpress
  links:
    - wordpress_db:mysql
  ports:
    - 8080:80
  volumes:  
    - ~/wordpress_dev/wp_html:/var/www/html
wordpress_db:
  image: mariadb
  environment:
    MYSQL_ROOT_PASSWORD: your_password_here
  volumes:
    - ~/wordpress_dev/wp_db:/var/lib/mysql

 

Otherwise, you can just download the file from here.

Let’s break down what these all mean, starting with the app container:

wordpress_app:

The name of your first container. This will be running the “web server” side of WordPress in our two container set-up.

image: wordpress

This tells docker-compose to use the “wordpress” image from the docker registry, which has all you need for a basic wordpress server.

links:
  - wordpress_db:mysql

This will link our separate container running mariadb to the wordpress_app container.

ports:
  - 8080:80

Designates port-forwarding for requests to “8080” to be directed to port “80” on the wordpress_app container. Basically allows us to hit localhost:8080 and view the site.

volumes:
  - ~/wordpress_dev/wp_db:/var/lib/mysql

Specifies a local directory on your environment to use for the /var/www/html directory of the container. This will allow us to have a directory in our “wordpress_dev” directory to use that’s connected to the docker container. And now the db container:

wordpress_db:

Name of the db container itself and start of the configuration settings of that container.

image: mariadb

This tells the container to use the mariadb image from the docker registry, letting us set up our database server in the container pretty easily.

  environment:
    MYSQL_ROOT_PASSWORD: your_password_here

This lets us specify an environment variable for the root MySQL user password, which is used when the mariadb service is configured in the container. Update this to something secure.

  volumes:
    - ~/wordpress_dev/wp_db:/var/lib/mysql

Same deal as above. Uses a directory under “wordpress_dev” to store database libraries. At this point, we can start up the containers. Do so by running the following:

docker-compose up -d

Which will start the containers in detached mode. Ensure they’re running with:

docker ps

Which will give you information on the two containers that we’ve created. Now that they’re created, we’re good to go. Just visit http://localhost:8080 and you should see the WordPress installation wizard. And that’s it. I might update this later with some more information on how git integrates with this set-up, but probably will do so via a different post. For now, you should be all set to access your local WP installation One extra tip is that if you need to access the WordPress installation’s database via mysql-cli, you can run the following:

docker run -it --link wordpressdev_wordpress_db_1:mysql --rm mariadb sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'

Which I’ve personally bound to a bash alias:

alias wp_mysqlcli="docker run -it --link wordpressdev_wordpress_db_1:mysql --rm mariadb sh -c 'exec mysql -h\"\$MYSQL_PORT_3306_TCP_ADDR\" -P\"\$MYSQL_PORT_3306_TCP_PORT\" -uroot -p\"\$MYSQL_ENV_MYSQL_ROOT_PASSWORD\"'"
%d bloggers like this: