Running Wordpress with Docker
Wordpress is one of the best, if not the best, content management / blogging systems available today. It is open source, self-hostable, and free to run. As with many of the open source projects, should you determine you'd rather have someone else manage your blog for you, then you can setup a blog at wordpress.com, and this helps support the open source efforts of the platform.
In this articla, and the associated video, I'll show you how to install and run Wordpress in Docker using Docker Compose. I'll also point out how to avoid some of the setup woes you might find if you wanted to switch to a wordpress with a domain / subdomain URL and SSL encryption.
What you'll need
- A server or machine to host your Wordpress and docker install.
- A domain name you'd like to use to access your Wordpress install.
- Docker (we'll go through the install on Ubuntu / Debian below).
- Docker-compose (again we'll go through the install on Ubuntu / Debian below).
- About 30 minutes.
- If you're running this from inside your local area network, and want to access it from the internet, you'll also want an understanding of Port forwarding, and a reverse proxy (I'll be using NGinX Proxy Manager).
Install Docker and Docker Compose
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
For Ubuntu 18.04 Bionic use the line below
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
For Ubuntu 20.04 Focal use the line below
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
sudo apt update
apt-cache policy docker-ce
sudo apt install docker-ce -y
sudo usermod -aG docker ${USER}
Now install docker-compose
sudo apt install docker-compose -y
Now Let's install the Wordpress and DB software
In your home folder create a new directory. Call it what you want, but just calling it "wordpress" is fine.
mkdir wordpress
Now, move into that directory:
cd wordpress
In this new directory, we want to create a file called "docker-compose.yml"
nano docker-compose.yml
In this file, we want to add the following code:
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: <a strong password>
MYSQL_DATABASE: <give a name that makes sense>
MYSQL_USER: <your name>
MYSQL_PASSWORD: <a password for your user>
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: <use the same user as above>
WORDPRESS_DB_PASSWORD: <use the same user password as above>
WORDPRESS_DB_NAME: <use the same db name as above>
volumes:
db_data: {}
Make sure to replace the text inside the < and > symbols. The fields in each section must match as below:
MYSQL_DATABASE = WORDPRESS_DB_NAME
MYSQL_USER = WORDPRESS_DB_USER
MYSQL_PASSWORD = WORDPRESS_DB_PASSWORD
NOTE: You may also need to change the left side of the port mapping (8000:80) from 8000 to a port that is available on your machine. If not, it's fine to leave it as 8000. Regardless of what you set it to, just remember what you set it to.
Now, save and exit nano with the following key combination:
CTRL + O, then Enter / Return to save.
CTRL + X to exit nano.
Let's Bring Up our Containers.
Now, we just need to run Docker Compose on our new .yml file.
docker-compose up -d
This should pull down the MySQL and Wordpress images from Docker, and link them together.
When you see it's done, indicated by 'done' being displayed after each container name, open a new browser tab, and enter the ip address and port number you set.
e.g. http://192.168.7.125:9021
is what I used...you should use your own machinne IP and port to ensure you see the 'Select language' page of the Initial Wordpress Install wizard.
Before we continue, we should run through this install wizard. So, fill in the relevant data, and when you get to the Success page. Click 'Login'.
Login to your fresh install, and you'll see the admin page for yoru wordpress system. You can now start theming, installing plugins, and more. If you only want to run this site locally, then you're done. If, however, you want to run this site with a domain name, we have a bit more to do.
Setting Up Wordpress for Access via Domain / Sub-Domain
I have setup a wildcard A record for my domain 'routemehome.org'. Essentially, just create an A record with an asterisk ( * ) as the subdomain, and then I point that to my home IP address. What this gives me is the ability to create subdomains for routemehome.org and have them immediately route to my home IP address without having to make an A record for each one.
Next, you'll want NGinX Proxy Manager installed and running on a machine in your network, or on the same machine ideally.
With NGinX Proxy Manger (NPM from now on) installed, we will create a new host, and point it to our install.
But first... we need to make a change to our Wordpress install in the settings.
In your Wordpress admin section, click Settings >> General. On that page, replace the Wordpress Page URL and Site URL fields with the domain URL you want in the end.
I setup a page called pihome.routemehome.org
. So, in those fields, I setup http://pihome.routemehome.org
Go ahead and set the other settings on that page, then click Save at the bottom of the form. Be aware that when you click Save, it will try to reload the page with the new URL, which we haven't setup yet, so it will give a 404 error. Don't sweat it, we'll fix it.
After you click save, let's go setup our NPM site. Inside NPM click to add a new Host. On the Details tab, enter the URL you want, in my case I entered pihome.routemehome.org
You should enter the name you want.
Next, enter the docker0 IP address of your machine if you are funning NPM and wordpress on the same machine, otherwise enter the local IP of the machine for your netowrk.
You can use the command ifconfig
or the command ip addr show
to find the interfaces, and their associated IPs.
Now, enter the port you set for your wordpress install. I used 9021, but you need to enter what you used... it was defaulted to 8000.
Click 'save', and now try your URL to make sure you are taking to your Wordpress home page (not the admin page).
If it worked, we are almost there. Now, edit the Host entry in NPM, and go to the SSL tab.
In the dropdown, select to Request a New Certificate. Click the 'Force SSL' option, enter your email address, and then click the 'Agree'. Now click Save again, and be patient. It may take a minute to get the SSL certificate.
If you don't get any error, and the pop-up window on NPM closes, try your URL again. It should now take you to the SSL encrypted page, and show the lock Icon in the browser bar.
Done! Good job. To access your admin page, just put the domain with /wp-admin at the end, and you can login to your install admin page.
e.g. https://yourwordpresssite.com/wp-admin
Now you can get busy building something amazing wtih wordpress.