Run WordPress and MySQL On Docker

In this blog post, we will learn how to run WordPress inside Docker containers on a Linux container host.

Step by Step Guide

About the Deployment

This deployment will use Docker compose which will deploy the following:

  • WordPress container running the latest WordPress build
  • MySQL container running MySQL 5.7b server
  • All the data that the containers are using is stored on volumes.
  • After deployment WordPress will be available on port 80 however you can change it the file under ports.

YML File

Below, is the YML file that contains all the configuration. You can change the values but make sure DB name-value matches in the DB and WordPress service.

version: '3.3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: enterpassword
       MYSQL_DATABASE: wp01
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     volumes:
       - wp_data:/var/www/html
     ports:
       - "80:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wp01
volumes:
    db_data: {}
    wp_data: {}

I saved the file as docker-compose.yml inside a directory called wp01 but you can name the directory any name you like.

Deploy

To deploy the solution, I will run the following command from the directory the file is located in.

docker-compose up -d

Configure WordPress

After I finished the deployment, I will access WordPress using my server IP address and the port I used.

http://serverip

Below you can see the configuration wizard.

After I finished with the configuration I logged in and updated the default post by adding a photo to it.

Volumes

If you run the command below, you will see that Docker composes created two volumes.

The first volume (wp01_db_data) is for the MySQL server

The second volume (wp01_wp_data) is for all WordPress images, etc.

docker volume ls

Check Status

To check the deployment status, I will use the command below.

docker-compose ps

Cleanup

To delete containers and leave the volumes attached I will run the command below. If you keep the volumes and deploy again, the configuration will stay the same and nothing will be lost.

docker-compose down

To delete the containers and volumes I will run the command below.

Docker-compose down --volume

This blog post was first published on our sister site https://www.ntweekly.com


Posted

in

by