Deploy WordPress With Docker and Traefik 2
I’ve written an article of using docker and Traefik to deploy WordPress a year and a half ago. There have been so many changes in the past 18 months, some information on my previous article seems to be inadequate or even wrong now. My plan is to rewrite all my Traefik related articles and use Traefik 2 with SSL enabled by default.
From this article, I will stop copying Traefik configuration files in each article. You can find the default configuration here.
Let me start with the docker-compose.yml
file.
1 | version: '3.7' |
Service : DB
Image:
There are two database images you can choose from, MySQL:5.7 or MariaDB. As the password authentication method changed in MySQL 8, if you really want to use MySQL, please choose version 5.7 or you need extra command^1 listed below.
1 | image: mysql:8 |
I don’t bother with extra settings so I will stick with MariaDB.
Volume:
I create named volume called wp-db-data
[^2] here. We use named volume for persistent data. You won’t lost your data when you upgrade WordPress nor even remove the container.
Network:
I add this database service to default
network to avoid unrelated containers from accessing this container. Docker use the folder name as prefix when it creates default network. For example my docker-compose file is in ~/wordpress
folder, docker will create default network as wordpress_default
.
Environment
I set root password, database name, username and password here.
Please do NOT use such simple username/password in production environment
Service : WordPress
depends_on:
WordPress will run after db
. If we stop both containers, WordPress will be stopped before db
.
Image:
I used the latest version of WordPress official image.
Environment
DB-HOST
will be the port 3306 of db
service. The other variables are just database name, username and password. Please make sure you use the same detail as we defined in db
service.
Volumes
I mount /var/www/html
from WordPress container to our local folder ./wp-data
. The benefit of doing that is we can modify files on our server directly. For example if we need to upload pictures, plugins or themes, we can put it in this folder straight away. In case your website is down due to bad plugin updates, you can also delete this plugin from here. There is no need to docker exec -it <mycontainer> bash
any more.
Network
I expose WordPress container to two networks. One is proxy
, it is for Traefik to find this container; the other one is default
which will link WordPress and DB together in the same network.
Label
This is the interesting part.
According to official Docker Service documentation,
- If a label defines a router (e.g. through a router Rule) and a label defines a service (e.g. implicitly through a loadbalancer server port value), but the router does not specify any service, then that service is automatically assigned to the router.
- If a label defines a router (e.g. through a router Rule) but no service is defined, then a service is automatically created and assigned to the router.
In summary, if a user does not want to link router to a service, nor define any service labels. Traefik will take care of everything including creating service and assigning service to router (host name).
Now we just need to run docker-compose up -d
, WordPress should be up in no time.
Thanks for reading, I hope this article can be of any help. Let me know if you have any questions.
[^2]: Please note we must define this named container at the end of docker-compose file. I also named this volume wp-db-data
, if we don’t use that, docker will add the folder name as prefix to your volume name. for example wordpress_db-data