This tutorial shows you how to deploy the official WordPress and MySQL Docker images using Docker Compose. It also demonstrates how to use Traefik as a reverse proxy so you can do things like rate-limit requests or restrict access to certain IP ranges.
Before we begin
First we need Docker and Docker Compose installed on our machine before we can do the steps in this tutorial. This tutorial shows you how to install it on Ubuntu 20.04.
Once you have installed Docker and Docker Compose, let’s begin by creating a directory to store our files.
Step 1: Create a Directory
In the following example, I will use wordpress-docker-demo.
Step 2: Create Docker Compose Config
Create a file called docker-compose.yml inside the directory.
Open the file and add the following contents.
As you can see, the config above defines three services. The first is the Traefik proxy, the second is WordPress using the php7.4-apache image and the last is MySQL.
There’s a couple of options you might want to change, which are:
- Change the MySQL root password assigned to the following variables WORDPRESS_DB_PASSWORD and MYSQL_ROOT_PASSWORD.
- Change the host value from wordpressdemo.net to your domain name.
If you are following along with this tutorial, you can keep the host as wordpressdemo.net and add it to your local hosts file like shown below.
sudo vim /etc/hosts
This will allow you to access the WordPress site from your local machine using the domain name rather than localhost.
Step 3: Docker Compose Up
Now let’s deploy WordPress by running the docker-compose command.
Wait until MySQL says ready for connections and the WordPress files are copied then check you can start the WordPress installation by browsing to
It works. You should now be able to install WordPress by completing the installation wizard.
How to Edit Files?
If you want to access the WordPress files inside the container from the host Docker is running on, you can create a folder called wordpress then add the following volume configuration to the bottom of the wordpress service.
Recreate the containers with
docker-compose down and
Now the files should be copied into the wordpress folder on your local host as you can see in the screenshot below.
Why Use Traefik?
You might be wondering why we need a reverse proxy when you can expose the WordPress container directly to the internet.
Some of the reasons you might want to use a reverse proxy in front of your WordPress docker container are:
In this tutorial, we saw how easy it is to deploy a WordPress blog using Docker with Docker Compose. It’s a great way to spin up a local development environment and it can also be used to deploy a site to production.