Introduction

In this article, we will see how to setup a WordPress website using Docker. Containerization has changed the face of how the applications are packaged and deployed. WordPress hosting using docker containers makes it easy to host and manage the hosting.

For setting up the website, a WordPress container and a SQL database container are required. As we are using the containers, configurations can be fed into the containers during the creation by defining them in the Docker compose file.

Prerequisites

A host with docker engine and docker-compose installed.

Here we are using Ubuntu 20.04 as the host. Docker and Docker-compose packages are installed using the below code.

Docker engine installation

The latest stable version of the docker can be installed by executing just 2 lines of code in your Linux shell. Detailed documentation is available at https://docs.docker.com/engine/install/ubuntu/

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

Add the current user (user ubuntu in my case) to the docker group

sudo usermod -aG docker ubuntu

Close the session and re-login for the user to update the permission

Docker-Compose installation

sudo apt install docker-compose -y

Docker network configuration

Two networks are defined for separating the web traffic and database connection.

sudo docker network create --subnet 10.10.10.0/24 data_default
sudo docker network create --subnet 10.10.11.0/24 proxy_default

WordPress and MariaDB Installation

As Docker loses its knowledge of data on reboot, the WordPress and the DB data has to be stored in a persistent storage (i.e. in a storage).

Following are the configuration details:

WordPress:

  • Container name – testweb_wp
  • local data folder ‘/opt/data/wp_testweb/html’ mapped to the wordpress container instance at ‘/var/www/html’
  • container port 80 is mapped to host port 8089
  • The container runs with the user account ubuntu with pid 1001(check your account puid and pgid and provide the number accordingly)
  • WordPress container connects to 2 bridge networks proxy_nw >> proxy_default and hosts_nw >. data_default
  • and the configurations to connect to the database

MariaDB:

  • Container name – testweb_db_host
  • There is no port mapping to the host machine as the it communicates internally to the wordpress container
  • local data folder ‘/opt/data/wp_test/db’ mapped to database container instance at ‘/config’

Create necessary folders

sudo mkdir /opt/data/ /opt/data/wp_testweb/ /opt/data/wp_testweb/html/ /opt/data/wp_testweb/db/

WordPress Docker Compose YAML

Create yaml file

cd /opt/data/wp_testweb/
sudo touch docker-compose.yaml

Use any text editor and add the following yaml code to the docker-compose file

version: '2'
services:
  testweb_db_host:
    image: linuxserver/mariadb:latest
    networks:
      - hosts_nw
    volumes:
      - /opt/data/wp_testweb/db:/config
    environment:
      - PUID=1001
      - PGID=1001
      - MYSQL_USER=wp_user
      - MYSQL_DATABASE=testweb_db
      - MYSQL_PASSWORD=Sup35ComplexPass
      - MYSQL_ROOT_PASSWORD=^3ryComplexPass
    restart: unless-stopped
  testweb_wp:
    image: wordpress:latest
    networks:
      - proxy_nw
      - hosts_nw
    ports:
      - 8089:80
    volumes:
      - '/opt/data/wp_testweb/html:/var/www/html'
    depends_on:
      - testweb_db
    environment:
      - PUID=1001
      - PGID=1001
      - WORDPRESS_DB_HOST=testweb_db_host
      - WORDPRESS_DB_USER=wp_user
      - WORDPRESS_DB_NAME=testweb_db
      - WORDPRESS_DB_PASSWORD=Sup35ComplexPass
    restart: unless-stopped

networks:
  proxy_nw:
    external: true
    name: proxy_default
  hosts_nw:
    external: true
    name: data_default

Powering UP the docker containers

The above configuration is to be saved as docker-compose.yaml and run following command from the saved folder.

docker-compose up -d

If your docker-compose fails with the following error,

ERROR: The Compose file './docker-compose.yaml' is invalid because:
networks.hosts_nw value Additional properties are not allowed ('name' was unexpected)
networks.proxy_nw value Additional properties are not allowed ('name' was unexpected)

The docker-compose has to be upgraded to the latest version. The latest stable version is now v2.1.1

sudo curl -L https://github.com/docker/compose/releases/download/v2.1.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Reboot the server for the changes to take effect and install the package gnome-keyring

# To reboot the server
sudo reboot
# Install gnome-keyring package
sudo apt install gnome-keyring

Now execute the command again “docker-compose up -d”

Summary

Now the containers are up and running. Verify with the command ” docker ps”

ubuntu@dockhost:/opt/data/wp_testweb$ docker ps
CONTAINER ID   IMAGE                        COMMAND                  CREATED          STATUS          PORTS                                   NAMES
46ae07cacda9   linuxserver/mariadb:latest   "/init"                  21 seconds ago   Up 17 seconds   3306/tcp                                wp_testweb-testweb_db_host-1
15c2f3ac5099   wordpress:latest             "docker-entrypoint.s…"   23 seconds ago   Up 16 seconds   0.0.0.0:8089->80/tcp, :::8089->80/tcp   wp_testweb-testweb_wp-1

Open any web browser and access the URL http://<ipaddress>:8089/

Well, you have your wordpress running !

Hope you like this article and thank you for reading.