Docker and wordpress for a better world
I am trying to represent a typical wordpress infrastructure
Isolation: every single wordpress share all with the others, filesystem, memory, database.
This lack of isolation causes different problems:
- The monitoring of each installation is harder.
- We share security problems
- We don’t have the freedom to work without the fear or blocking 100 customers
We are overwhelmed by the problems
it is an operating-system-level virtualization environment for running multiple isolated Linux systems (containers) on a single Linux control host.
Wikipedia helps me to resolve one problem (theory), container is isolated Linux System
Docker borns as wrap of LXC container but now we use an own implementation runc to serve your application ready to go in an isolate environment, with own filesystem and dependencies.
Worpdress in this implemetation has two containers, one to provide apache and php and one for mysql database. This is an example of Dockerfile, it describes how a docker container works it is very simple to understand, from this example there are different keywords
FROMdescribes the image that we use as start point.
RUNrun a command.
EXPOSEdescribes ports to open during a link, in this case MySql runs on the default port 3306.
CMDis the default command used during the run console command.
Very easy to read, it is a list of commands! We are only write a container definition, now we can build it!
Download this images
We are ready to run all! Dockerfile is only a way to describe each single container, and the pull command downloads online container ready to work, it is a good way to reuse your or other containers.
We downloaded mysql and wordpress, with the run command we start them and we define our connections
I can try to explain this commands, it run two containers:
- The name of the first container is mysql and it uses the
mysqlimage, we use -p flag to expose mysql port now you can use phpmyadmin or other client to fetch the data but remember that is not a good practice.
- The second container called wp1 uses the image
gianarb/wordpressforward the container port 80 (apache) on host 8080, that in this case it is the way to see the site. –link flag is the correct way to consume mysql outside the main container, in this particular case we could use wp.database.prod how url to connect at mysql from our worpdress container, awesome!
- Docker image supports environment variable
ENVfor example we can use them to configure our services, in this case to set root password in mysql and to configure worpdress’s database connection
We are ready! Now you have a worpdress ready to go on port 8080.
To save time and to increase reusability we can use
that helps us to manage multi-container infrastructures, in this case one for
mysql and one for wordpress.
In practice we can describe all work did above in a
Now we can run
To prepare and start our infrastructure. Now we have one wordpress with own mysql that run on port 8081. We can change wordpress port to start new isolate wordpress installation.
In Cloud with AWS ECS
We won a battle but the war is too long, we can not use our PC as server. In this article I propose AWS Elastic Container Service a new AWS service that helps us to manage containers, why this service? Because it is Docker and Docker Composer like, it’s managed by AWS, maybe there are more flexible solutions, Swarm, Kubernetes but it is a good start point.
A services of keywords to understand how it works:
- Container instance: An Amazon EC2 that is running the Amazon ECS Agent. It has been registered into the ECS.
- Cluster: It is a pool of Container instances
- Task definition: A description of an application that contains one or more container definitions
- Each Task definition running is a Task
- Create a cluster
- Up nodes (one in this case)
- Push your first task!
- Follow the status of your tasks
You can use another docker-compose.yml with a different wordpress port to build another task with another worpdress!
Now is only a problem of URL
We are different isolated worpdress online, but they are an ip and different ports, maybe our customers would use a domain name for example. I don’t know if this solution is ready to run in production and it is good to run more and more wordpress but a good service to turn and proxy requests is HaProxy. This is an example of configuration for our use case:
wp1.gianarb.it and wp1.gianarb.it are two our customers and 126.96.36.199:8080, 188.8.131.52:8081 are our wordpress.
Note: This configuration increase the scalability of our system, because we can add other service in order to support more traffic.
There are other solutions
- Consul to increase the stability and the scalability of our endpoint