A WordPress Setup With Docker, PHP 7.4, Apache 2.4 and MySQL 8 on Ubuntu 20.04 LTS

Are you a web developer working on another WordPress site? If so, you may want to run a site locally and safely on your laptop to play around with a theme or plugin first, or you want to experiment with this or that cool feature with no fears on breaking the live site.
Today’s post is especially helpful in 2020 and 2021 with so many radical, disruptive changes taking place in the WordPress community mainly introduced by the development of Gutenberg, the new content editor for WordPress 5.0.
Gutenberg aims to be a game-changer in the way WP sites are built. Some argue it resembles Medium or Wix in the way content is created, but judging by the debate users have had so far, the future of the new editor’s adoption might not seem too clear yet.
Be that as it may, in today’s post let me guide you through the process of running a WordPress site locally with the help of a specific, custom Docker set up.
Prepare the Docker Environment
All you need to start off is a copy of the WordPress site you’re working on in the form of a ZIP file as well as a copy of its database as an SQL backup, then clone this GitHub repo:
$ git clone git@github.com:programarivm/wpdocker.git
Unzip your ZIP file into the wpdocker
directory:
$ unzip /home/standard/Downloads/programarivm.zip -d /home/standard/projects/wpdocker
Create an .env
file:
$ cp .env.example .env
Make sure to comment out or remove the CREATE DATABASE
statement, if any, from your SQL script and then copy it into docker/mysql/backup.sql
$ cp /home/standard/Downloads/backup.sql /home/standard/projects/wpdocker/docker/mysql/backup.sql
Build the Docker containers:
$ docker-compose up --build

Import the Database Backup
With the Docker containers running already, SSH into wp_mysql
to import the database backup:
$ docker exec -it wp_mysql /bin/bash
root@7ed4c31aa253:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.31 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use wpdocker;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> source /home/backup.sql
Add a New Entry to Your Hosts File
Find out the IP address of your Apache container:
$ echo $(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' wp_httpd)
With the IP at hand, add the following entry to /etc/hosts
:
172.18.0.3 wpdocker.local
As you can see my IP is 172.18.0.3
this time, but of course don’t forget to replace that one with your actual IP instead.
Edit Your WordPress Configuration File
Find out the IP of your MySQL container:
$ echo $(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' wp_mysql)
And update your wp-config.php
file accordingly as follows:
define('DB_NAME', 'wpdocker');
define('DB_USER', 'bob');
define('DB_PASSWORD', 'password');
define('DB_HOST', '172.18.0.4');
Once again, as you can see my container’s IP is 172.18.0.4
this time, but don’t forget to replace that one with your actual IP instead.
Update the URLs
The URLs must be changed at this stage, which can be done in several different ways. Below it is shown how to do this directly in the database by running these two queries in the MySQL prompt.
UPDATE wp_options SET option_value='http://wpdocker.local' WHERE option_name='siteurl';UPDATE wp_options SET option_value='http://wpdocker.local' WHERE option_name='home';
Set up File Permissions
Finally set up permissions in order to enable the installation of new plugins, themes, and so on.
sudo chown standard:www-data -R wp-content
sudo chmod 775 -R wp-content
The step-by-step process described above worked for me on Ubuntu 20.04 LTS, and hopefully you’ll find something useful that can be applied to your particular set up and situation too! If you’re already familiar with Docker your site will be up and running in minutes.
In my opinion the present how to guide may definitely be a good alternative to installing WordPress 5 with other popular stacks like XAMPP, WAMP and MAMP, particularly if working as a part of a team.
Thanks so much for reading! For now that’s all I have to say about moving a live Gutenberg WordPress site from live to localhost. I hope you’ll stay tuned for the next episode about installing a self-signed SSL certificate to enable HTTPS.