Deploying Wordpress and MySQL with Docker Swarm

Fred Lynch
3 min readNov 3, 2024

--

Overview

In this article, we will discover how to transition a web infrastructure to a containerized environment using Docker Swarm. Docker Swarm is a perfect tool to help a company stay flexible and efficient in a digital environment.

Let’s use the scenario that our online publication reports on the hottest updates and trends in IT and wants to stay ahead of the competition. You’ve chosen to use Docker Swarm to quickly and securely respond to rapidly changing news cycles and efficiently serve your growing readership with responsiveness, even as traffic fluctuates.

Electronic publishing tools such as Wordpress and MySQL orchestrated by Docker Swarm will allow your company to quickly adapt to changing customer demands, scale up or down based on traffic, and ensure uninterrupted service availability. Let’s get started!

Step 1: Initiate Swarm Cluster

Note: We will be using the 3-Node Swarm from our previous project where we first created a swarm. For reference, click on the 3-Node Swarm link.

Step 2: Set up Networks (Frontend and Backend)

We’ll create two networks in Docker Swarm: one for the frontend (WordPress) and another for the backend (MySQL). Think of this as a restaurant where the front end is where people eat (Wordpress) and the backend is where the food is cooked (MySQL). This separation helps keep things organized and secure.

A. In the managing node create the frontend and backend networks by running the following command:

docker network create — driver overlay frontend-network
docker network create --driver overlay backend-network

Step 3: Setting up MySQL Service

A. Create a Docker Volume by running the following command:

docker volume create mysql_data

This volume acts as MySQL’s persistent storage, meaning if the container restarts, data remains safe here.

B. Deploy MySQL Service by running the following command:

docker service create \
--name mysql \
--network backend \
--replicas 1 \
--mount source=mysql_data,target=/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=your_password_here \
mysql:latest
Make up a Password and store it for later

Step 4: Deploy the WordPress Service

In this setup, WordPress will serve as our “front door” where people will publicly see and engage with our information, which is stored on the “back-end” (MySQL) where it remains secure and accessible to the editors.

A. Deploy a WordPress Service by running the following command:

docker service create \
--name wordpress \
--network frontend \
--network backend \
--replicas 3 \
-p 80:80 \
-e WORDPRESS_DB_HOST=mysql \
-e WORDPRESS_DB_USER=root \
-e WORDPRESS_DB_PASSWORD=your_password_here \
wordpress:latest
Be sure to replace <your_password_here> with the appropriate password!

B. Verify that each service is running by executing the following command:

docker service ls

C. To verify that your WordPress site runs correctly, copy the public IP address of the managing instance and paste it into your web browser.

Congratulations!!!

You’ve just successfully containerized a WordPress publication system using Docker Swarm, demonstrating how modern businesses can efficiently manage and scale their web applications. By setting up a three-node cluster with separate frontend and backend networks, along with persistent storage for the database, you’ve created a robust, scalable infrastructure that can handle growing traffic while maintaining high availability — exactly what a growing publication like Level Up Publications needs.

--

--

Fred Lynch
Fred Lynch

Written by Fred Lynch

Husband | Father | Programmer | Creative | Artist

No responses yet