Wordpress setup with Docker and Traefik03 Jun 2018
For this weekend project I had to migrate a couple of wordpress websites away from an existing server that I’m going to decommission soon. The current environment consists of a traditional setup with nginx and php-fpm running as systemd services on a CentOS VM with each website being configured within its own server block. This was, of course, an opportunity to come up with a new environment where I could experiment with some components that I’ve recently learned about.
So let’s go ahead and put down the requirements for this new environment and see what I can come up with:
- The system does not need to be highly available, this is a single node environment. Since the workloads are mostly personal blogs of my lovely wife and other relatives it’s acceptable that in the eventuality the system goes kaput the websites can stay down until I manually recover from backups.
- It’s important that the new environment is easy to reproduce. This is related to 1/ as in case the server disappears for whatever reason I need to be able to quickly bring up the same environment on a different machine and recover the databases and document root files from backups.
- Last but not least this is an environment where I want to play and experiment. First I want to try out Træfik which is a new load balancer and reverse proxy that I’ve read about few days ago and looks promising. Second I want to practice the containerized way of applications packaging and see how it fits an operational workflow(keeping the system up to date, applying patches, backup/restore, etc). Since the workloads that are running on the system are not mission critical but in the same time I cannot afford to get my wife upset by keeping her blog unavailable for days this is the perfect environment where I can learn and get some skin in the game.
Enough with the talk now let’s move to actions and see how to set up the environment.
- Get the cheapest VM from Hetzner
- 3EUR/month for 1vCPU, 2GB RAM, 20GB disk space(on local SSD). I’ll use CentOS as OS but the instructions here(except installing the docker package) should be pretty much the same as the magic happens inside the container.
Once the server gets online install Docker per the official docs
- Prepare containers persistent storage directories.
- We want the application data, in our case the wordpress files and the databases, to be stored on a persistent location so we use the bind mount feature to expose host directories inside the containers.
- Create a docker network for the internal containers communication.
At this point we’re ready to start creating the containers.
Prepare the Traefik container configuration file.
- Create the Traefik loadbalancer container
- Create the MariaDB database container
- Create wordpress00 website
- Create wordpress01 website
- This is a snapshot of the Traekif loadbalancer dashboard. All that I had to do is to create the toml configuration file and run the container. It’s quite awesome given that all you need to do when creating a new wordpress container is to pass the specific labels and it gets autodetected and configured by Traefik. Pretty cool stuff.
Note: at this point the wordpress containers run uninitialized Wordpress installations. I won’t cover the restore part from my current environment in this post to keep it simple for now.
I am using Cloudflare for CDN and hosting the DNS zone for my domain. At this point I am ready to set the DNS records and point the websites to this new server. Cloudflare does SSL termination by default so I’m configuring http to https redirection from inside the Wordpress application.
The entire process has been extremely fast and I love that. You can see in the recording below that even if the commands were run manually the entire procedure took just few minutes. I know that in the end the complexity doesn’t disappear but instead it’s just moved to the Dockerfiles. I have split feeling in regards to this - on one hand I’m not feeling comfortable about not knowing what’s going inside the container but on the other hand I feel that an entire community around a certain project can do a great job at maintaining the Dockerfile. One thing is for sure - containers are such a convenient way of shipping the application.
Træfik. I got a quick glimpse at its capabilities and I like it a lot. I am curious to dig deeper into its configuration details and more complex deployments such as HA or Kubernetes integration.
I can declare this a productive Sunday. Feel free to reach out if you have any questions!
- Below is a recording of the entire process