A friend of mine has a startup and is after a Wordpress site that is easy to maintain. His startup is looking at developing a suite of small web applications to compliment the site, with that in mind I chose to build it with Docker, as it low maintenance, easy to change hosting providers and easy to host applications side by side.
What is Docker
Docker makes it easier to develop and deploy applications by using containers (which are kinda like virtual machines, but more lightweight). Docker containers are built from images, which contains all the configuration required to create a container. A lot of common software applications already have pre-made Docker images, for example MySQL and Wordpress. These images can be easily pulled down and extended to rapidly create applications, without having to manually install software locally. With Docker, applications can be bundled with it’s dependencies within a single easy-to-use image. This image can be distributed using a registry (a package management system for Docker).
To set up a traditional Wordpress, you would spin up a server with Digital Ocean or another provider, install MySql, PHP, NGINX. Then install Wordpress and configure NGINX to point to it. If something were to happen to that server and a new one is required, you would have to do the whole process again. Additionally if you were to hand over the server to someone else to maintain, then they would have to figure out how everything works.
Docker containers provides a way to abstract the complexity of the application, allowing software, such as Wordpress, to run in an isolated environment. As the Docker documentation puts it, a container is a standardised unit of software in a lightweight, standalone and executable fashion. This also bridges the gap between development and production, as the same containers can run on any machine that has Docker installed.
By abstracting away the logic of the application into containers, the deployment and maintenance of the application becomes easier. Additionally there are tools such as Docker Compose and Kubernetes that manages groups of containers, allowing a complex application with multiple containers to be deployed easily.
To set up a working Wordpress instance using Docker and Docker Compose is as simple as writing a docker-compose.yml file, defining the containers you want, then running
Anatomy of a Docker Image
As previously mentioned, a Docker image contains the configuration required to build a Docker container. Docker images are written as a Dockerfile, a simple example docker file is written below:
The application consists of the following parts:
- Docker Host, which is running the following containers
- Wordpress: The actual wordpress application
- MySQL: The database engine that wordpress uses
- Nginx: The reverse proxy used for forwarding the requests the Wordpress
- LetsEncrypt: Used to handle SSL certificate generation for HTTPS
Its worth talking about why NGINX and LetsEncrypt are not running as Docker containers. Its possible to run them within container, however since belong more to the system, rather than the application, its better to have them installed outside Docker. In other words, NGINX and LetsEncrypt can be swapped out for any reverse proxy, for instance Apache or a managed AWS service.
With this separation, the Docker containers don’t care about how the requests come to the server (i.e. handling HTTPS, etc.), decoupling the application from the transport logic.
So with all that in mind, the architectural diagram will look like this:
This was designed in mind to allow the one server to host multiple applications. Adding new applications is as easy as spinning up the Docker containers for the new service and then adding a rule to NGINX to proxy connections to it.
Deploying the application is as follows:
- Setup the server
- Install Docker
- Clone the repository
- Setup the containers by running
docker-compose up -d(The
-dlaunches the containers in the background)
- Install NGINX and LetsEncrypt
- Edit the NGINX configuration to proxy requests to the port Wordpress is on
- Setup LetsEncrypt to do the SSL/HTTPS handling
Updating the application is also dead simple with Docker, the steps are:
- Shut down the docker containers (
git pullwithin the repo
- Restart the containers
Using Docker removes a lot of the complexity of maintaining an application, updating software is as simple as bumping the version number in the Docker Compose file and rebuilding the images.
Docker also provides simple and repeatable deployments, making it easy to redeploy an application on another server if need be. Even through Docker is another tool to learn, it’s value pays off quickly especially when it is for an application that will receive infrequent updates. For a website like Wordpress, updating MySQL and Wordpress is as simple as bumping the version numbers.