This is a beginners level exercise for hosting a two-tier architecture on Docker Containers. WordPress is a monolithic application which was designed to work on shared servers, however to utilize the advantages of micro services, we need to segregate the data layer and the processing layer.
The first split is to split the database and the web application handled by WordPress. The architecture that we are going to deploy is as follows
To deploy this architecture, we are going to use the following Docker community images.
Mysql — Official Image | Docker Hub
Wordpress — Official Image | Docker Hub
First of all, we can search the images, using the docker search command.
Then we can pull the desired images using docker pull
docker pull mysql
docker pull wordpress
The next step is to create a custom network
docker network create — subnet 192.168.10.0/24 — driver bridge webapp
The network can be verified in the ip addr command as well.
Now we have a custom network created, we can spin up our containers with the required configuration
docker run -it — name wpdb_mysql — network webapp -e MYSQL_ROOT_PASSWORD=LHSWCVAbzS -d mysql:latest
We are going to use a custom port 8081 to expose it to the world for our WordPress server. The other parameters include the configuration of mySql database server. Node that as we are using a custom bridged network, in the host, I have just provided the container name as it would be accessible by name as well.
docker run — name wp — network webapp -p 8081:80 -e WORDPRESS_DB_HOST=wpdb_mysql -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=LHSWCVAbzS -e WORDPRESS_DB_NAME=wpdb -d wordpress
Now, you can access your freshly installed website on your favorite browser as well.
Conclusion
In this tutorial, I just covered splitting up of the database and web application provided by WordPress using containers in a custom bridged network. Please provide your feedback for any scope of improvements.
Top comments (1)
Hi this is very interesting. Do you have any views on how best to deploy this? For example could you deploy this architecture to Google Cloud Run?
I’m thinking of you did deploy this to a serverless environment then you would have to work on the two containers locally to make edits (eg add new posts or themes etc) as - given docker is stateless - you would lose any changes made while the site was actually deployed.
Dose this make sense? (As I’m only a beginner!)