Creating a Robust WordPress and MySQL Architecture

Deepak Sharma
9 min readSep 7, 2023

--

In my previous article, we learned how to build a three-tier architecture with Docker, utilizing WordPress as the main building block. The best platform for customers to create and publish their blogs and articles is WordPress, which is recognized for its user-friendly interface and content management features. To protect the sensitive data of our clients, we used a MySQL database that enabled smooth data management and permanence.

Link to my previous Blog

Challenge in Last Blog:- In my last blog there is a small challenge. If mysql container goes down then we can launch it in 1 second by docker. But we know the IP is dynamic. So there is a high chance that the next time the IP of the MySQL container changes and wordPress loses connectivity with the MySQL container. In this case, no one can connect with our website. The client cannot see blogs & articles.

Solution:- There are two ways to solve this challenge:-

  1. Data Linking
  2. ABC

# Data Linking:-

Data linking is a way that enables communication and data sharing between Docker containers. It allows one container to securely connect to another container’s network and access the services or resources provided by that container.

When containers are linked, Docker provides a mechanism for environment variables to be set in the consuming container, which contains information such as the hostname, IP address, and exposed ports of the linked container. This information allows the consuming container to interact with the linked container as if they were on the same network, even though containers are isolated from each other by default.

Drawbacks of Data Linking in Containers

There are some drawbacks of data linking.

  1. Security Concerns: While container linking attempted to provide isolation between containers, there were still potential security concerns. Linked containers could potentially access each other’s internal processes and files, which might be a security risk in some scenarios.
  2. Scaling and Load Balancing: Container linking did not inherently support load balancing or scaling. If multiple instances of a service needed to be scaled, managing the links and ensuring consistent communication across instances could be challenging.
  3. Limited Network Control: Container linking did not offer advanced networking capabilities, such as defining custom networks or controlling traffic routing. This could be problematic when trying to achieve more complex network topologies or integrate with external networks.
  4. Maintainability: As the number of linked containers increased, managing the configuration and ensuring proper communication between containers became increasingly complex and difficult to maintain.
  5. Cross-Host Communication: Container linking was primarily designed for communication within a single Docker host. When containers needed to communicate across different hosts, the limitations of container linking became even more apparent.
  6. It only goes one way. As a result, if we link d1 with d2, only d2 can ping d1 and d1 cannot ping d2.
  7. If we use the container name to ping one container to another, the IP address is first internally converted before the ping is sent to the other container. If the container’s IP changes for whatever reason, In this situation, the container name is still pointing to the older IP of the container. Therefore, the WordPress container and the MySQL container lost communication.

Demo of MySQL & WordPress Setup with Data Link

Step-1 Launch MySQL DB

$ docker run -dit  --name db -v /data:/var/lib/mysql/  -e MYSQL_ROOT_PASSWORD=redhat  -e MYSQL_USER=deepak -e MYSQL_DATABASE=mydb  -e MYSQL_PASSWORD=deep@200  mysql:latest

Now We launch WordPress Container linking with MySQL container with the help of Data Linking

Step-2 Launch WordPress

$  docker run -dit --name wp  -p 8080:80  --link  db  wordpress:latest

Step-3 Access our applications

We can access our WordPress container from outside. For this, we need to use the EC2 instance public IP and 8080 port number. It will ask for some information about the database. So that WordPress can connect with MySQL DB.

Here you can see that we are not using mySQL container IP, we are using MySQL container name “db”. The container name never changes.

Step-4 Create Account & Publish Blog

We may now make an account, log in to our account, and write and publish articles. Your articles are now available to anybody via the Internet.

After publishing the article, we can access our article until MySQL & WordPress containers have connectivity. http://13.233.166.91:8080/2023/08/28/mysql-reliable-setup/

Step-5 Checking the Reliability of Our Setup

Data link gives facility to WordPress container to connect MySQL container with the help of MySQL container name “db”. When we use the data link concept, we can ping the MySQL container from inside the WordPress container. Internally first it converts db(name of MySQL container ) into 172.17.0.2 (IP of MySQL container). Then the packet is sent between containers.

There is a small drawback in the data link setup. If the IP of the MySQL container changes for any reason, then the “db”(MySQL contanier_name) is still pointing to the older MySQL container IP. So WordPress lost connectivity & client wouldn’t see any articles.

To prove that the data link is not reliable for communicating WordPress and MySQL containers. Basically, we need to change the MySQL container IP and check whether WordPress is able to connect to MySQL or not.

For this, I am going to delete the MySQL container and launch some other containers. Because docker gives IP in sequence (e.g.:- 172.17.0.2, 172.17.0.3, 172.17.0.4,172.17.0.5, …..) .

docker inspect db | grep IPAddress

The IP of MySQL Container is 172.17.0.2. Now I delete the MySQL container and launch a random container. I want the 172.17.0.2 Ip allocated to this new container. So when I launch MySQL container time MySQL container would get different IP.

docker rm -f db                         //to delete MySQL container
docker run -dit --name test centos:7
docker inspect test | grep IPAddress

We can see the new container got IP 172.17.0.2. So MySQL container will get a different IP . Now I am going to launch the MySQL container again and check IP.

$ docker run -dit  --name db -v /data:/var/lib/mysql  -e MYSQL_ROOT_PASSWORD=redhat  -e MYSQL_USER=deepak -e MYSQL_DATABASE=mydb  -e MYSQL_PASSWORD=deep@200  mysql:latest
$ docker inspect db | grep IPAddress

We can see that the MySQL container IP is changed. Now let’s try to access our previous articles. But we are getting an error

It means WordPress lost connectivity with MySQL even though we use the data link concept. Hence proves that the data link method is not a reliable way to communicate two containers.

Let’s see a better way to connect two containers

# Create Custom Network

Docker has plugins. With the help of plugins, docker provides many facilities like storage, networking, and much more. We will use the bridge plugin and create a custom network. Docker provides three networks pre-created.

We will use a bridge driver and create a new network. Basically, a network means a LAN setup. By default, two networks are isolated. Let’s first understand about the bridge.

Bridge:- It is called L3-switch. The bridge is a device that is created by software. It behaves like both a switch and a router. It provides the functionality given below:-

1.  IPAM (DNS + DHCP)      //explained IPAM below 
2. Behave like both Switch + Router
3. Provides outside connectivity to containers (SNAT enabled)
  • All routers have two IPs:- one is a public IP and the other is a Private IP. All routers have a program enabled that provides an IP address to the container as soon as it connects to the router. This program is called the DHCP program.
  • DNS program provides a facility that OS can ping to each other by their names. The bridge provides an IPAM facility. IPAM means a combination of both DNS and DHCP facilities. That’s why containers can also ping each other by their names in the bridge network.

Now we will use a bridge driver and create a new network. So that we will get bridge functionalities in our network

Demo :- Resilient setup of WordPress & MySQL

Step-1 Create a custom network

docker network create --driver bridge  --subnet 10.1.2.0/24  deep-net
docker network ls

It is important to give a subnet (IP range) to the DHCP program. Because this range will decide how many containers can launch in our network. For example:-

192.168.0.0/24:- 2⁸ → 256 containers can launch in a network

192.168.0.0/16:- 2¹⁶ → 65536 containers can launch

Sometimes it happens that our containers keep on failing. Because we utilized all IPs. So it is better to plan before creating a network infrastructure.

Step-2 Launch MySQL container in deep-net network

$  docker run -dit  --name dbos  --network deep-net  -v /data:/var/lib/mysql/
-e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_USER=deepak -e MYSQL_DATABASE=mydb -e MYSQL_PASSWORD=deep@200 mysql:latest

This dbos container launched in the deep-net network.

Step-3 Launch WordPress container in deep-net network

$  docker run -dit --name wp  -p 8080:80 --network deep-net  wordpress:latest

Step-4 Connect WordPress with MySQL

We can access our WordPress container from outside. For this, we need to use the EC2 instance public IP and 8080 port number. It will ask for some information about the database. So that WordPress can connect with MySQL DB.

We launched both containers MySQL & WordPress in our custom network. That’s why both containers can connect to each other by container name and the container name never changes.

Now we can create an account and publish our blog & articles. This is a resilient setup. So if the MySQL container goes down then we wouldn’t lose our data. We can launch the MySQL container again and get our data back.

Also, we are using container names to connect. So it doesn’t bother if MySQL container IP changes.

Conclusion:-

We were facing a challenge that the MySQL container IP might change and we would lose connectivity between the WordPress container and MySQL Container . To solve this challenge, we can use two methods:- Data Linking and creating a custom network and launching MySQL & WordPress containers inside it.

In the first method (Data Linking), we use the MySQL container name to connect both containers. But if the IP of the MySQL container changed, then we lost connectivity. Because the data link concept has some drawbacks. If the IP changes, but it doesn’t update the DNS and container name still point to the older IP of the MySQL container. That’s why we lost connectivity.

We use the second method. In this method, we create a new network and launch both MySQL and WordPress containers inside it. Both containers are in the same network. So they have connectivity. Also, our custom network gives functionality of IAM. IPAM means we get the power of both DNS & DHCP. So we can connect to containers by container names & if the container IP changes, then DNS would update the newer IP. So we will not lose any connectivity between MySQL and WordPress containers & we get a resilient setup of MySQL and WordPress.

Thank you for reading …

--

--

Deepak Sharma
Deepak Sharma

Written by Deepak Sharma

0 Followers

5x RedHat Certified Engineer (EX200, EX294, EX180, EX280, RH358) || DevOps Engineer || Docker, K8s, Ansible, Git & Github , Gitlab, Terraform || Jenkins || AWS

No responses yet