Run WordPress and MySQL On Docker Linux Host

In this blog post, I will show you 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:

  • a WordPress container running the latest WordPress build
  • a 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

Posted

in

,

by

Comments

One response to “Run WordPress and MySQL On Docker Linux Host”

  1. Daniel Avatar
    Daniel

    hi, i’m using fedora 32 with podman, i have this error:
    Traceback (most recent call last):
    File “/usr/local/bin/podman-compose”, line 8, in
    sys.exit(main())
    File “/usr/local/lib/python3.8/site-packages/podman_compose.py”, line 1093, in main
    podman_compose.run()
    File “/usr/local/lib/python3.8/site-packages/podman_compose.py”, line 611, in run
    self._parse_compose_file()
    File “/usr/local/lib/python3.8/site-packages/podman_compose.py”, line 666, in _parse_compose_file
    compose[‘_dirname’]=dirname
    TypeError: ‘str’ object does not support item assignment

    ¿can you help me? thanks a lot