Building my portfolio site, I thought I’d show you how I set up my WordPress development using Docker. Given the way my current site architecture is set up, it is the process flow that I am currently using now but it will most certainly change in future as I continue to learn more about Docker containerisation and Kubernetes and transition the server away from traditional hosting environment more to one that is containerised and can be replicated with instances.
So my current portfolio site, runs on a typical LAMP stack:
It is currently hosted on Amazon Lightsail VPS pointing to the ap-southeast-2 region. I am also using Git for version control. Setting up the LAMP environment for me is currently a manual task, I am hoping to rid this as I continue to improve my procedures.
The annoy thing thing I find when I need to work on templating and content changes for WordPress is the need to sync my live site with my local development site. The reason a sync needs to be made is because:
FYI, I am working on a Mac OS machine. In essence, here’s how I currently have my process flow set up to do WordPress development using Docker:
Yes there’s a few steps here that could be improved but I will improve this during my spare time.
To quickly connect to my production server, I add an entry to my ~/.ssh/config file:
# Contents of ~/.ssh/config Host pt HostName User IdentityFile /path/to/special/privatekey/amazon.pem
So now when I need to sync changes from production, I simply run these commands in terminal:
# Dump SQL to local from remote
ssh pt mysqldump -p -u mysqluser mysqldb > c.sql
# Sync remote to local folder
rsync -auH pt:/var/www/html .
I now have my latest database changes locally (in a dump file) and any new code changes will be tracked in Git locally.
Using docker build, users can create an automated build that executes several command-line instructions in succession. Here are my relevant docker files:
DockerFile
FROM wordpress:php7.2-apache RUN apt-get update && \ apt-get install -y --no-install-recommends ssl-cert && \ rm -r /var/lib/apt/lists/* && \ a2enmod ssl && \ a2ensite default-ssl EXPOSE 80 EXPOSE 443
docker-compose.yml
version: '3' services: wp: build: . ports: - "80:80" - "443:443" volumes: - ./html:/var/www/html depends_on: - mysql mysql: image: "mysql:5.7" ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: root MYSQL_PASSWORD: $MYSQL_PASSWORD MYSQL_DATABASE: mysqluser MYSQL_USER: mysqluser volumes: - .:/docker-entrypoint-initdb.d
They key points to note in these files are:
Configure your local wp-config.php file so it has these settings:
define('DB_NAME', getenv('MYSQL_DATABASE'); define('DB_USER', getenv('MYSQL_USER'); define('DB_PASSWORD', getenv('MYSQL_PASSWORD'); define('DB_HOST', 'mysql://localhost');
Everything should now be setup for you now to run:
docker-compose up -d
Your WordPress site should load successfully when you access https://localhost in your browser.
If your WordPress site has full URL links to assets such as images (e.g. https://petertran.com.au/wp-content/uploads/2018/07/16/image.jpg), they won’t load on your local development till you change your /etc/hosts file to have an entry that matches your live production domain name. To do this, run the following in terminal:
sudo echo "127.0.0.1 " >> /etc/hosts
After you complete your code changes, commit your code locally using git.
Make a database dump now:
docker exec _wp-mysql_1 mysqldump source_dbname > ./local/path/to/dump.sql
I use Git to pull in the latest code changes and run a few SSH commands to import a local MySQL dump file to the remote MySQL server. The process goes like this:
ssh pt 'git pull' ssh pt 'mysql -u username -p dest_dbname -e "EMPTY DATABASE dest_dbname;"' ssh pt 'mysql -u username -p dest_dbname' < ./local/path/to/dump.sql
Now that your new changes are live, you can log into the WordPress admin on your live site to clear any caches you may have, do a visual check and functional test of your new changes.
A lot of these steps are quite manual and could be scripted further to make easy. Some changes to this workflow I would like to do are possibly:
Did you follow this guide by any chance? If you did, I’m curious to hear about how you went or what improvements you would make to this workflow. Let me know in the comments!
I have been fortunate in working a lot more with Kubernetes lately over the last few months so I've been…
Frustrated with getting blank images in Puppeteer Chrome screenshots, recently I was in a situation where I needed to migrate…
It has been six year since my last major revamp/redesign of my current petertran.com.au portfolio site. So what are my…
This article helps to solve a Bad owner or permissions on .ssh/config issue occurring on a Windows 10 machine when…
Only a few lines of code required is to implement a payment system, thanks to inline Pin Payments integration. Accepting…
Here is a relatively easy way to move your VirtualBox virtual machine (VM) images to another location or drive: Shutdown all…