Rolf Kleef, drostan.org

Rolf Kleef

Making internet work for civil society.

library_books Software development expand_more

Wordpress

Overview

I am using a devcontainer to work on a Wordpress site locally. My current setup runs these services:

wordpress

An Apache server, as well as a devcontainer that inludes WP-CLI, with a local source directory added as bind mount.

db

A MySQL server, with its database stored on a Docker volume.

pma

A PhpMyAdmin server, to manage the database(s).

Diagram

Configuration

Directory structure

.
|-- .devcontainer (1)
|   |-- apache-vhost.conf
|   |-- devcontainer.json
|   |-- docker-compose.yml
|   `-- Dockerfile
|-- public_html (2)
|   `-- ...
|-- .env (3)
`-- ...
1 The .devcontainer contains the configuration for Docker and VS Code.
2 The public_html directory contains the Wordpress site.
3 The .env file contains environment variables used in the Docker configuration for Wordpress.

I keep my development environment and Wordpress deployment together. In the setup of the devcontainer, Apache is configured to serve the Wordpress site from the public_html directory.

I like to also keep a checkout out git repository on my host. This allow me to update things through the Wordpress admin interface, and then commit the changes to git.

I have seen different hosting providers offer ways to deploy a git repository to a website. However, this typically "disconnects" the repository from the files themselves.

I prefer a scenario where my hosted Wordpress site automatically installs security updates. With ssh access, I can then commit those changes to the repository.

Docker configuration

Docker Desktop reads the .env file when starting up services. These variables can then be passed to a container.

docker-compose.yml
services:
  wordpress:
    build:
      context: ..
      dockerfile: .devcontainer/Dockerfile
    depends_on:
      - db
    env_file: (1)
      - ../.env
    ports:
      - "${WORDPRESS_PORT}:80"
    extra_hosts: (2)
      - localhost:host-gateway
    volumes:
      - ..:/app

  db:
    image: mysql:8.0
    environment:
      MYSQL_DATABASE: ${WORDPRESS_DB_NAME}
      MYSQL_ROOT_PASSWORD: ${WORDPRESS_DB_ROOT_PASSWORD}
    volumes:
      - mysql-data:/var/lib/mysql

  pma:
    image: phpmyadmin
    depends_on:
      - db
    ports:
      - "${PMA_PORT}:80"
    environment:
      PMA_USER: ${WORDPRESS_DB_USER}
      PMA_PASSWORD: ${WORDPRESS_DB_PASSWORD}
      UPLOAD_LIMIT: 100M
      MYSQL_ROOT_PASSWORD: ${WORDPRESS_DB_ROOT_PASSWORD}

volumes:
  mysql-data:
1 For the wordpress container, we want to pass almost all environment variables from the .env file. Therefore, the .env file is used to pass everything to the container.
2 The extra_hosts setting is used to let the Apache server in the container reach the host via localhost.

When using Docker Desktop on Linux, directories and files mounted into a container are owned by user root. Docker Desktop maps between the local user on the host, and root in the container.

However, the Apache server in the container runs as user www-data. It is therefore not possible to install or upgrade Wordpress or its plugins from the Wordpress admin interface.

Therefore, VS Code accesses the devcontainer as root too. The WP-CLI utility makes it possible to do code maintenance on Wordpress and plugins. This requires the --allow-root option.

$ wp --allow-root plugin install hello-dolly
Twitter LinkedIn Github Gitlab Meetup RSS Miro Trello Zotero Hypothesis