Introduction:
Setting up WordPress as microservices using Docker involves breaking down WordPress into distinct services (e.g., database, and WordPress application) and managing them with Docker containers. In this post, we will go through a Docker Compose configuration that sets up a WordPress site in minutes.
What is Docker Compose?
Docker Compose is a tool for defining and running multi-container Docker applications. Using a YAML file, you can configure your application’s services, networks, and volumes, making the process of setting up and managing containers much easier.
The Docker Compose YAML File
Here’s the configuration file we’ll be explaining:
version: '3.8'
services:
wordpress_db:
image: mysql:5.7
container_name: wordpress_db_container
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
volumes:
- db_data:/var/lib/mysql
networks:
- wordpress_network
wordpress:
image: wordpress:latest
container_name: wordpress_container
ports:
- "8080:80" # Expose WordPress on host port 8080
volumes:
- wordpress_data:/var/www/html
environment:
WORDPRESS_DB_HOST: wordpress_db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
networks:
- wordpress_network
networks:
wordpress_network:
volumes:
wordpress_data:
db_data:
Custom Network wordpress_network will enable to access website from host. Data will be saved on persistent storage, for this to happen create wordpress_data and db_data directory on the same location as docker-compose.yml file.
Understanding the Compose File
1. Version Definition The file starts with the version 3.8
, ensuring compatibility with modern Docker Compose features.
version: '3.8'
2. Services: Building the Application
This Compose file defines two services: a database service (wordpress_db
) and the WordPress application itself (wordpress
).
- Database Service The database service uses the MySQL 5.7 image, with predefined credentials and a persistent storage volume to retain data.
wordpress_db:
image: mysql:5.7
container_name: wordpress_db_container
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
volumes:
- db_data:/var/lib/mysql
networks:
- wordpress_network
WordPress Service The WordPress service connects to the database and exposes the application on http://localhost:8080
. Persistent storage ensures customizations like themes and plugins aren’t lost.
wordpress:
image: wordpress:latest
container_name: wordpress_container
ports:
- "8080:80"
volumes:
- wordpress_data:/var/www/html
environment:
WORDPRESS_DB_HOST: wordpress_db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
networks:
- wordpress_network
3. Networking A custom network, wordpress_network
, ensures secure communication between the WordPress and database containers.
networks:
wordpress_network:
4. Persistent Storage Named volumes (wordpress_data
and db_data
) store WordPress files and database records, preventing data loss.
volumes:
wordpress_data:
db_data:
Conclusion
With this docker-compose.yml
file, deploying WordPress becomes straightforward. Simply run docker-compose up -d
to start the application. Enjoy the simplicity of Docker Compose as it handles the heavy lifting for you!
Add a Comment