Wordpress (with MySQL)

Introduction

WordPress is a web content management system. It was originally created as a tool to publish blogs but has evolved to support publishing other web content, including more traditional websites, mailing lists and Internet forum, media galleries, membership sites, learning management systems and online stores.

MySQL is an open-source relational database management system. Its name is a combination of "My", the name of co-founder Michael Widenius's daughter My, and "SQL", the acronym for Structured Query Language. A relational database organizes data into one or more data tables in which data may be related to each other; these relations help structure the data.

The Docker Compose File

---
version: "3.8"

services:
  mysql:
    image: mysql:latest
    container_name: mysql
    hostname: mysql
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    env_file:
      - .env
    environment:
      - TZ=${TZ}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      timeout: 20s
      retries: 10
    ports:
      - 3306:3306
    volumes:
      - /opt/wordpress/database:/var/lib/mysql
      - /etc/localtime:/etc/localtime:ro
    networks:
      - wordpress_network
    labels:
      - com.centurylinklabs.watchtower.enable=true

  wordpress:
    image: wordpress:latest
    container_name: wordpress
    hostname: wordpress
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    env_file:
      - .env
    environment:
      - TZ=${TZ}
      - WORDPRESS_DB_HOST=${WORDPRESS_DB_HOST}
      - WORDPRESS_DB_NAME=${WORDPRESS_DB_NAME}
      - WORDPRESS_DB_USER=${WORDPRESS_DB_USER}
      - WORDPRESS_DB_PASSWORD=${WORDPRESS_DB_PASSWORD}
    ports:
      - 80:80
    volumes:
      - /opt/wordpress/config:/var/www/html
      - /etc/localtime:/etc/localtime:ro
    depends_on:
      mysql:
        condition: service_healthy
    networks:
      - wordpress_network
    labels:
      - com.centurylinklabs.watchtower.enable=true

networks:
  wordpress_network:
    name: wordpress_network
    external: true

docker-compose.yml

The Docker Environment Variables File

TZ=Europe/Stockholm
MYSQL_DATABASE=db
MYSQL_USER=user
MYSQL_PASSWORD=password
MYSQL_ROOT_PASSWORD=secretpassword
WORDPRESS_DB_HOST=mysql
WORDPRESS_DB_NAME=db
WORDPRESS_DB_USER=user
WORDPRESS_DB_PASSWORD=password

.env

This Docker Compose file defines two services, the app is named “wordpress” and the database is called “mysql”. The services are built from the latest Docker images. The container names ares set to “wordpress” and “mysql” as well.

The “restart” section ensures that the container will always be restarted if it exits (unless manually stopped). This is useful for ensuring that the services are always available, especially if they crash or are shut down unexpectedly.

The “security_opt” section prevents your container processes from gaining additional privileges. This is an important security consideration.

The “ports” section for wordpress maps port 80 on the host machine to port 80 in the container. This allows us to access the wordpress web interface from our local machine. The MySQL database maps to its standard port of 3306.

The “volumes” section maps the “/var/www/html” and “/var/lib/mysql” directories inside the containers to local directories on the host machine. This allows us to persist data across container restarts. Wordpress stores all its configuration data in its own directory and MySQL stores its database in its own directory, so it’s important to keep it intact.

The "networks" section defines a new Docker network for this stack to run in called "wordpress_network".

The "labels" section allows the Watchtower service to keep this container update automatically.

Running Wordpress with Docker Compose

To run Wordpress with Docker Compose, first, make sure you have Docker and Docker Compose installed on your machine. Then, create a new directory for your project and save the above Docker Compose file as “docker-compose.yml” in that directory along with the environment variables as “.env”.

Next, run the following command from the same directory:

docker compose up -d

This command will start the home-assistant container in the background and detach from it. You can then access the Wordpress web interface by going to http://localhost in your web browser.

Conclusion

In this article, we explored how to set up and run the Wordpress software using Docker Compose. We looked at the different sections of the Docker Compose file and explained how they work together to create a functional Wordpress service. By running Wordpress with Docker Compose, you can easily deploy and manage the tool on your own server, without having to worry about dependencies or configuration.


https://wordpress.com

MySQL