Deploy a WordPress Blog with Docker Compose and Traefik
By Tony Mackay ·
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.
mkdir ~/wordpress-docker-demo
cd ~/wordpress-docker-demo
Step 2: Create Docker Compose Config
Create a file called docker-compose.yml inside the directory.
touch docker-compose.yml
Open the file and add the following contents.
version: '3'
services:
traefik:
image: traefik:v2.3.6
restart: always
command:
- --accesslog
- --api.insecure=true
- --providers.docker
- --providers.docker.exposedbydefault=false
volumes:
- /var/run/docker.sock:/var/run/docker.sock
ports:
- 80:80
- 443:443
- 8080:8080
wordpress:
image: wordpress:php7.4-apache
depends_on:
- mysql
restart: always
environment:
- WORDPRESS_DB_HOST=mysql
- WORDPRESS_DB_USER=root
- WORDPRESS_DB_NAME=wordpress
- WORDPRESS_DB_PASSWORD=password
expose:
- 80
labels:
- traefik.enable=true
- traefik.http.routers.wordpress.rule=Host(`wordpressdemo.net`)
mysql:
image: mysql:8.0.22
restart: always
environment:
- MYSQL_DATABASE=wordpress
- MYSQL_ROOT_PASSWORD=password
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.
docker-compose up

Wait until MySQL says ready for connections and the WordPress files are copied then check you can start the WordPress installation by browsing to http://wordpressdemo.net
.

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.
volumes:
- ./wordpress:/var/www/html
Recreate the containers with docker-compose down
and docker-compose up
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:
- You can rate limit incoming requests.
- You can restrict access to the wp-login.php to certain IP ranges.
- Load balance to multiple backend WordPress instances.
- Perform rolling or canary upgrades.
- Host multiple WordPress sites on the same server.
- Generate Lets’ Encrypt SSL certificates automatically.
Conclusion
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.
Read Next
- The Best Books to Learn Virtualization, Linux and Automation
- Recommended Tools and Software for System Administrators
- How to Rate Limit Requests to WordPress Using Traefik
- Using Traefik to Route HTTP Requests to Multiple Docker Microservices
Tony is the founder and editor of GraspingTech, a blog that helps developers and business owners deploy modern web applications to the cloud. He has written over one hundred tutorials which have been read by more than a million people. Some of the topics he covers are Linux, Virtualization, DevOps and web development.