WordPress with MySQL and phpMyAdmin in Docker.
Most applications, on an architecture level, comprise of more than one component. For example – WordPress has two main components: the WordPress installation, as well as the database. This is the bare minimum required, and does not include the rest of the environment on top of which this runs. To host WordPress you need a vps / server with PHP, MySQL and Apache. And if you are migrating your WordPress from one host to another, you will also need to import your WordPress database into the new instance, which will require phpMyAdmin or similar interface.
Docker is a well known container management system that is used to manage containerized applications. Docker-compose is a toolkit provided by docker to manage an application that has multiple components. Docker makes running these stacked applications easy by combining it in a multi-container Docker application by using Docker-compose files. We take a look at how we can run WordPress with MySQL and phpMyAdmin using Docker.
- Server / VPS running Ubuntu (any version after 20.04)
- Docker installed on your server
- Docker-compose installed
If you do not have a VPS or Dedicated Server you can get it from ServerFarm, as they have very competitive pricing.
Writing the docker-compose file
When writing a docker-compose file, it’s always good to begin with the end in mind. What is the goal we are trying to achieve? What problem do we want to solve? It is a good idea to define this first, so that you work with some form of structure. We will be running three different services here: WordPress, MySQL and phpMyAdmin. This means our service stack will comprise out of 3 images networked together. So for the project, we will need:
- networks (to network the images together)
- mounted volumes for the data
The first thing to define in this YAML file is the version of the compose file to use. This should be the Docker-compose latest version.
From here I usually create a rough draft of what I plan to do:
From here we can start expanding the code for each service stack that we require. It is important to remember that your spacing should be correct, else the file will not run.
Starting with the database, we have some variables that need to be defined and unique. You will notice that some of these will be used by all three service stacks, so it will be necessary to write them down. You need to create the following variables for the database connection:
We can do this by putting the variables in a .env file that goes in the root with our docker-compose.yaml file:
This will then be used by each service that needs it.
Now we can define our database. We will be using the latest MySQL image, and mount the data volume as /var/lib/mysql. We will also call the .env file with our database connection settings. In order to have communication between the images, we will define a network as well.
Our docker-compose.yaml file will now look like this:
The next service we will define is phpMyAdmin. phpMyAdmin is a database management tool for MySQL compatible databases. As a portable web application written primarily in PHP, it has become one of the most popular MySQL administration tools, especially for web hosting services. This is needed when we work with databases – for example to import and export a MySQL database using a GUI.
As with MySQL, we will always use the latest version of phpMyAdmin. We are using some of the variables in the .env file, so will pull that into the service.
The final service we need to define is WordPress. This image will come with PHP and Apache, but we will need to connect it to the database and put it on the same network as MySQL and phpMyAdmin. As before, we will be using the .env file for this as well, as the database connection settings is defined there.
This is it.
You can now run this file with docker-compose to create a WordPress, MySQL and phpMyAdmin stack in Docker.
One thought on “WordPress with MySQL and phpMyAdmin in Docker.”
Pingback: Setting up a cluster of docker servers with Ansible - Sleuth Technical