Install WordPress with Docker

WordPress Docker Setup Files: Example for Local Development

This article shows an example of WordPress Docker setup files for local development on either Windows, MAC or Ubuntu.

The installation tool kit and setup files include:

  • WordPress files
  • MariaDB/MySQL used for WordPress database
  • phpMyAdmin interface to connect to your MySQL database
  • WP-Cli: WordPress Command Line Interface
  • Makefile directives for automatization.

You will learn:

  • WordPress Docker directory structure
  • Docker-compose and WP-Cli configuration files
  • Installing WordPress using docker-compose


Contents:

Step 1. Download a WordPress Docker setup template

Download a template from Github at: kassambara/wordpress-docker-compose

git clone https://github.com/kassambara/wordpress-docker-compose
cd wordpress-docker-compose

Project directory structure:

files/wordpress-docker-compose
├── LICENSE
├── Makefile
├── README.Rmd
├── README.md
├── config
│   └── php.conf.ini
├── docker-compose-onlinehost.yml
├── docker-compose.yml
├── mysql
├── wordpress
├── wp-auto-config.yml
└── wpcli
    ├── Dockerfile
    ├── Makefile
    └── entrypoint.sh

Essential folders:

  • mysql: MySQL database files for MariaDB
  • wordpress: WordPress web files
  • wpcli contains a Dockerfile example to build WordPress CLI.

Essential files:

  • .env file: contain the environment variables required for the wordpress installation
  • docker-compose.yml: WordPress docker compose application services
  • Makefile: Set of simple bash command lines to build, install and configure WordPress, as well as, to start and stop the docker containers.

Step 2. Inspect the content of the configuration files

We’ll inspect the following configuration files:

  1. ./docker-compose.yml: Defines docker services for running wordpress
  2. ./wplci/Dockerfile and ./wpcli/Makefile: Provides command lines options to manipulate wordpress database
  3. ./Makefile: Provides make commands to start, install and autoinstall wordpress.

docker-compose configuration file

Inspect the content of the docker-compose.yml file, which defines 4 services:

  • WordPress/PHP
  • MySQL database
  • phpMyAdmin interface to the database
  • wpcli: WordPress command line interface
  • healthcheck, which check whether services (MySQL, wordpress) are ready
version: '3.6'
services:

  wordpress:
    image: wordpress:${WORDPRESS_VERSION:-latest}
    container_name: ${COMPOSE_PROJECT_NAME:-wordpress}
    volumes:
      - ./config/php.conf.ini:/usr/local/etc/php/conf.d/php.ini
      - ${WORDPRESS_DATA_DIR:-./wordpress}:/var/www/html
    environment:
      - WORDPRESS_DB_NAME=${COMPOSE_PROJECT_NAME:-wordpress}
      - WORDPRESS_TABLE_PREFIX=${WORDPRESS_TABLE_PREFIX:-wp_}
      - WORDPRESS_DB_HOST=${WORDPRESS_DB_HOST:-mysql}
      - WORDPRESS_DB_USER=${DATABASE_USER:-root}
      - WORDPRESS_DB_PASSWORD=${DATABASE_PASSWORD:-password}
    depends_on:
      - mysql
      - phpmyadmin
    restart: always
    ports:
        - 80:80

  mysql:
    image: mariadb:${MARIADB_VERSION:-latest}
    container_name: ${COMPOSE_PROJECT_NAME}_mysql
    volumes:
      - ./mysql:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=${DATABASE_PASSWORD:-password}
      - MYSQL_USER=${DATABASE_USER:-root}
      - MYSQL_PASSWORD=${DATABASE_PASSWORD:-password}
      - MYSQL_DATABASE=${COMPOSE_PROJECT_NAME:-wordpress}
    restart: always

  phpmyadmin:
    depends_on:
      - mysql
    image: phpmyadmin/phpmyadmin:${PHPMYADMIN_VERSION:-latest}
    container_name: ${COMPOSE_PROJECT_NAME}_phpmyadmin
    restart: always
    ports:
      - ${PHPMYADMIN_PORT}:80
    environment:
      PMA_HOST: mysql
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-password}

  # Command line interface to wordpress
  wpcli:
    build: ./wpcli/
    image: wpcli
    container_name: ${COMPOSE_PROJECT_NAME}_wpcli
    volumes:
      - ${WORDPRESS_DATA_DIR:-./wordpress}:/var/www/html
    working_dir: /var/www/html

# Check availability of essential services
  healthcheck:
    image: wpcli
    build: ./wpcli/
    container_name: ${COMPOSE_PROJECT_NAME}_healthcheck
    command: sh -c "/wait"
    environment:
      - WAIT_HOSTS=mysql:3306, wordpress:80
      - WAIT_BEFORE_HOSTS=${WAIT_BEFORE_HOSTS:-30}
      - WAIT_AFTER_HOSTS=${WAIT_AFTER_HOSTS:-15}
      - WAIT_HOSTS_TIMEOUT=${WAIT_HOSTS_TIMEOUT:-300}
      - WAIT_SLEEP_INTERVAL=${WAIT_SLEEP_INTERVAL:-30}
      - WAIT_HOST_CONNECT_TIMEOUT=${WAIT_HOST_CONNECT_TIMEOUT:-30}

Note that, under the wordpress application, we map the ./config/php.conf.ini into the container so that the PHP picks up our additional configuration settings specified below. This makes it possible to increase the upload limit.

Contents of: ./config/php.conf.ini

file_uploads = On
memory_limit = 512M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 600

Note also that, the provided docker-compose.yml file includes the phpMyAdmin application. If you want to use the admirer interface for your database, then add the following directives in your docker-compose.yml file in lieu of the phpMyAdmin directives.

  adminer:
    image: adminer
    restart: always
    links:
      - mysql
    ports:
      - 8080:8080

WP Cli Dockerfile and configuration file

The WordPress cli is built from the following Dockerfile, which includes the docker-compose-wait tool. The wait tool makes it possible to wait for a container to be ready before running another container. This makes wp-cli wait for MySQL to be ready before manipulating the database.

WP Cli Dockerfile (path: ./wpcli/Dockerfile):

FROM wordpress:cli

# Install make tool
USER root
RUN apk add --no-cache make

# Make docker-compose wait for container dependencies be ready
# Add the wait script to the image
ENV WAIT_VERSION 2.7.2
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/$WAIT_VERSION/wait /wait
RUN chmod +x /wait

# Add Makefile to scripts dir
ADD Makefile entrypoint.sh /scripts/
RUN chmod +x /scripts/entrypoint.sh

ENTRYPOINT [ "/scripts/entrypoint.sh" ]
USER 33:33
CMD ["wp", "shell"]

This Dockerfile is based on wordpress:cli, installs the docker-compose-wait and has an entrypoint, which can either execute WP-cli or a Makefile.

Note that, the wordpress:latest and wpcli images both run with the user www-data, but the problem is that the individual www-data users have different user-id’s:

  • user-id=33 in wordpress
  • user-id=82 in wpcli

We have to make sure they both use the same user-id. This can be solved by having the wpcli run with the USER 33:33 in the Dockerfile.

The WP Cli tool automates the installation of WordPress using the following Makefile (path: ./wpcli/Makefile):

install: configure

configure:

    @echo "⚙️ Configuring WordPress parameters..."
    wp core install \
        --url=${WORDPRESS_WEBSITE_URL_WITHOUT_HTTP} \
        --title=$(WORDPRESS_WEBSITE_TITLE) \
        --admin_user=${WORDPRESS_ADMIN_USER} \
        --admin_password=${WORDPRESS_ADMIN_PASSWORD} \
        --admin_email=${WORDPRESS_ADMIN_EMAIL}

    wp option update siteurl ${WORDPRESS_WEBSITE_URL}
    wp rewrite structure $(WORDPRESS_WEBSITE_POST_URL_STRUCTURE)

Using this Makefile, the WP-Cli tool will configure:

  • URL and title of website.
  • URL structure.

Make commands for automatic WordPress setup

Path: ./Makefile

start:
    docker-compose up -d --build

healthcheck:
    docker-compose run --rm healthcheck

down:
    docker-compose down

install: start healthcheck

configure:
    docker-compose -f docker-compose.yml -f wp-auto-config.yml run --rm wp-auto-config

autoinstall: start
    docker-compose -f docker-compose.yml -f wp-auto-config.yml run --rm wp-auto-config

clean: down
    @echo "💥 Removing related folders/files..."
    @rm -rf  mysql/* wordpress/*

reset: clean

Thanks to this Makefile, you will be able to automatically install and configure wordpress using make autoinstall.

Step 3. Edit the WordPress Docker setup environment variables

A .env file has been included to easily set docker-compose variables without having to modify the docker-compose.yml configuration file itself.

Default values have been provided as a mean of getting up and running quickly for testing purposes. It is up to the user to modify these to best suit their deployment preferences.

Open the .env file and update the contents if you want. For example, just change the project name and your password (for database and wordpress admin).

# Open the file
nano .env

Contents:

# 1/ Project name -------------------------------------------------
# Must be lower-case, no spaces and no invalid path chars.
# Will be used also as the WP database name
COMPOSE_PROJECT_NAME=wordpress

# 2/ Database user and password -----------------------------------------
# Set non-root database user if wanted (optional)
DATABASE_PASSWORD=password
DATABASE_USER=root

# 3/ For wordpress auto-install and auto-configuration -------------------
WORDPRESS_WEBSITE_TITLE="My Blog"

# URL
WORDPRESS_WEBSITE_URL="http://localhost"
WORDPRESS_WEBSITE_URL_WITHOUT_HTTP=localhost
WORDPRESS_WEBSITE_POST_URL_STRUCTURE="/blog/%postname%/"

# Website admin identification. Specify a strong password
WORDPRESS_ADMIN_USER="wordpress"
WORDPRESS_ADMIN_PASSWORD="wordpress"
WORDPRESS_ADMIN_EMAIL="your-email@example.com"

# 4/ Software versions -----------------------------------------------
WORDPRESS_VERSION=latest
MARIADB_VERSION=latest
PHPMYADMIN_VERSION=latest

# 5/ Ports: Can be changed -------------------------------------------
PHPMYADMIN_PORT=8080

# 6/ Volumes on host --------------------------------------------------
WORDPRESS_DATA_DIR=./wordpress

# 7/ Healthcheck availability of host services (mysql and woordpress server)
# Waiting time in second
WAIT_BEFORE_HOSTS=5
WAIT_AFTER_HOSTS=5
WAIT_HOSTS_TIMEOUT=300
WAIT_SLEEP_INTERVAL=60
WAIT_HOST_CONNECT_TIMEOUT=5

# 8/ Used only in online deployement --------------------------------------
WORDPRESS_WEBSITE_URL_WITHOUT_WWW=example.com
PHPMYADMIN_WEBSITE_URL_WITHOUT_HTTP=sql.example.com

Step 4. Install WordPress using docker compose

You can automatically deploy a local docker wordpress site in 5 minutes using the following commands. Two options are available: make command and docker-compose standard commands.

# Download a wordpress docker-compose example
git clone https://github.com/kassambara/wordpress-docker-compose
cd wordpress-docker-compose

# Use make command for automatic installation and
# configuration of wordpress
make autoinstall

# Or, use docker-compose standard commands
docker-compose up -d --build
docker-compose -f docker-compose.yml -f wp-auto-config.yml run --rm wp-auto-config
  1. Visit your wordpress website at http://localhost. Default identification for admin (http://localhost/wp-login.php):
  • Username: wordpress and
  • Password: wordpress

Website

  1. Visit your database via phpMyAdmin at http://localhost:8080
  • Username: root and
  • Password: password

phpMyAdmin

Step 5. Shutdown and cleanup

# Stop and remove containers
docker-compose down
# Build, and start the wordpress website
docker-compose up -d --build
# Reset everything
docker-compose down
rm -rf mysql/* wordpress/*

Note that, instead of using the above docker-compose commands, you can also use easily the following make shortcut command lines if you have Unix systems (MAC / Linux).

# Build, and start the wordpress website
make start
# Stop and remove wordpress docker containers
make down
# Reset everything
make reset

Summary

This article describes the WordPress Docker setup and associated configuration files to install and run WordPress using docker-compose



Version: Français

(Next Lesson) WordPress Local Development Using Docker Compose
Back to Install WordPress with Docker

Comments ( 9 )

  • DriverMan

    Hey Guys. This was the best docker for wp development guide that I found so far. It teaches you everything and as a linux noobie It was sooooo good…
    Keep Up The Good Work guys

    • Kassambara

      Thank you much for the positive feedback, highly appreciated!

  • Fernando Bueno

    I just have love and gratitude for the amount of great practices and great tech put together to solve an old pain

    • Alboukadel

      Thank you for the positive feedback, highly appreciated

  • Jhorgint

    hi my friend. Thanks for this amazing post, my question: how should I use or run wp-cli?
    I tried for example:
    # wp –info
    /bin/sh: 2: wp: not found

  • Jean Ibbotson

    Yes agreed this was so helpful and it actually works. Thanks so much. Now I just need to secure with ssl certs.

  • Muhwezi Gerald

    Good work. Kindly guide me on how I can add pre-installed plugins & theme – activate them after WordPress set-up. Plugins or themes from either Github or WordPress plugins source.

  • Szabolcs Kocsis

    Hi, thanks, it is a very useful article. When new release is availabe for wordpress, and I would like to stop and relaunch it, the app will get the latest image from compose? It is not clear for me!

Give a comment

Want to post an issue with R? If yes, please make sure you have read this: How to Include Reproducible R Script Examples in Datanovia Comments

Teacher
Alboukadel Kassambara
Role : Founder of Datanovia
Read More