MySQL and Wordpress Services via Docker Swarm!
Integral Concepts
- Docker: An open-source platform that enables developers to package, distribute, and run applications within isolated, lightweight containers. These containers can run on any operating system.
- Docker Container: Self-contained units that package software and its dependencies, enabling application deployment across diverse environments.
- Docker Image: Standalone, executable packages that contain the essentials to run software. This includes the code, runtime, libraries, and dependencies. Images serve as the foundation for containers.
- Docker Compose: A tool for defining and running applications. It uses YAML configuration files to specify the services, networks, and volumes required for your application.
- Docker Stack: A collection of services defined in a single Docker Compose file that work together to form an application. It allows you to deploy and manage multi-container Docker applications as a single unit.
- Docker Swarm: Docker’s native clustering and orchestration tool. It enables you to deploy and manage multi-container applications across a cluster of machines. It provides features such as service scaling, rolling updates, and load balancing.
- Docker Secrets: A feature to sensitive data like passwords or API keys. Encrypted at rest and in transit, thus allowing authorized services access without using plain text or environment variables.
- Replica: A copy of a container that runs concurrently with the original container. Each replica serves the same purpose as the original container, enabling scalability, fault tolerance, and efficient resource utilization in distributed application environments.
- YAML: A human-readable data serialization language commonly used for configuration files. It uses indentation to represent data structures. It supports key-value pairs, lists, and nested structures.
- IDE: An Integrated Development Environment (IDE) is a software tool that combines code editing, debugging, and project management features to facilitate software development.
- MySQL: An open-source relational database management system (RDBMS) that uses Structured Query Language (SQL) for managing and querying data.
- WordPress: An open-source content management system (CMS) used for creating websites and blogs.
Prerequisites
- An IDE
- I use VS code - A docker swarm cluster
- Click here to read an article detailing how to create one - Terminal access
Compose File
In this demo, the swarm is already set up. From here, the only thing left is to create the compose file. Docker secrets will be leveraged to secure the passwords for the MySQL and word press services. The secret will need to be in a file within the same directory as the containers. This will be accomplished by the following steps:
# Create a new directory with the specified name and move into it.
mkdir [directory-name] && cd [directory-name]
# Example
mkdir blue && cd blue
# Create an empty Docker Compose YAML file
touch docker-compose.yml
# Create an empty file to store the root password for the database
touch db_rootpassword.txt
# Create an empty file to store the password for the database
touch db_password.txt
The passwords can be anything you like. To populate them run the vi command.
vi db_rootpassword
In the vi program, press i to enter the insert mode. type any password you like. Press the ESC key, then :x to exit the vi editor. Do the same for the other password file.
I like to utilize VS code’s formating asistance, thefore i generally write the code in VS code, then copy it into the docker-compose file in the container once finished. This also makes it easier to push all my compose files to GitHub.
Before writing any code in the file, save it as a docker-compose.yml file.
Now, The goal of this docker-compose.yml is to do accomplish the following:
- Create 2 networks. One for frontend and another for backend.
- Create a single replica MySQL service associated with the backend network
- Make the database service resilient and its data persistent across container restarts, using a Docker Volume.
- Create a 3 replica Wordpress service associated with the frontend network
- The Wordpress service will be accessible from the browser using port 80
- The Wordpress service will be associated with the MySQL service.
The gist for this file is found below. I recommend creating one youself while using mine as a reference. These goal can be accomplished many ways and mine is in no way the ‘best’ or ‘right’ way to do it. Docker hub is your friend use it to find info like default ports or volume mount locations
Once your docker-compose.yml is complete, copy it. Go back to the terminal and run the vi command on the docker-compose.yml. Paste in this file, then close.
Deploy The Stack
Run
docker stack deploy -c docker-compose.yml [desired stack name]
# Example pictured below
Running
docker service ls
will list all of your services
If you’re interested, you can run the following command in a terminal for each node to see which service is being run on them
docker container ps -a
Alternatively, you can run the following command in the manager terminal for each service to where their replicas are being ran
docker service ps [first three character of service ID]
# Example based on previous image
docker service ps 5td
Verify Connection
Open your brower and go to
http://[manager-instance-public-IPv4]:80
# Example pictured below
You will either be brought to a login screen or an installation screen. Either way, fill in the user info from the docker-compose.yml
- Upon filling in the info on the installation screen, you should get a message along the lines of ‘ Wordpress already installed’. Click login
CONGRATS!! 🎉
You now have all you need to run a cool Wordpress website!