Docker Compose streamlines the management of complex applications running on multiple containers. This article delves into the world of docker-compose.yaml, the configuration file that acts as the blueprint for your multi-container application. We’ll then use this knowledge to craft a docker-compose.yml specifically for a WordPress application.
Part 1: Unveiling docker-compose.yaml
Introduction
Docker Compose allows you to define and orchestrate multi-container applications. It eliminates the need to manage individual containers manually, simplifying development and deployment workflows.
The Power of docker-compose.yaml
The docker-compose.yaml file acts as the central configuration file for your Docker Compose application. It specifies everything your application needs to run smoothly, including:
- Services (Containers): Defines the individual containers that make up your application.
- Configurations: Sets specific parameters for each container, such as ports, environment variables, and resource allocation.
- Volumes: Defines how data persists between container restarts, ensuring your application doesn’t lose information.
- Networks: (Optional) Creates custom networks for container communication within your application.
Structure and Syntax
A docker-compose.yml file is written in YAML, a human-readable data serialization language. It uses key-value pairs and indentation to define various sections. Here’s a basic structure:
version: "X.Y" # Specify the docker-compose.yml file format version
services:
# Define individual services (containers) here
service_name:
image: image_name:tag # Specify the Docker image to use
ports: # Map container ports to host machine ports
- "container_port:host_port"
volumes: # Persist data between container restarts (optional)
- "host_path:container_path"
environment: # Set environment variables for the container (optional)
VAR_NAME: value
volumes: # Define volumes for persistent data (optional)
volume_name: {}
networks: # Define custom networks for container communication (optional)
network_name: {}
Part 2: Building a WordPress Docker with docker-compose.yml
Prerequisites
To follow along, ensure you have Docker installed and running on your system. A basic understanding of YAML syntax is also helpful.
Step-by-Step Walkthrough
- Creating the docker-compose.yml:Open your terminal and navigate to your project directory. Create a new file named
docker-compose.yml
using your preferred text editor. - Specifying the Version:Add the following line at the beginning of your file to specify the docker-compose.yml file format version. A commonly used version is “3.8”:YAML
version: "3.8"
- Defining Services:The
services
section defines each container that makes up your WordPress application. Here, we’ll define two services:wordpress
for the WordPress application itself anddb
for the MySQL database.
services:
wordpress:
# ... (service configuration for WordPress)
db:
# ... (service configuration for MySQL database)
Building the WordPress Service:Under the services
section, define the wordpress
service with the following configuration
services:
wordpress:
image: wordpress:latest # Use the official WordPress image
ports:
- "80:80" # Map container port 80 (WordPress) to host port 80
volumes:
- ./wp-content:/var/www/html/wp-content # Persist wp-content directory
Creating the Database Service:Next, define the db
service for the MySQL database:
This configuration specifies the wordpress:latest
image, which pulls the official WordPress image from Docker Hub.The ports
section maps container port 80 (where WordPress runs) to host port 80, allowing you to access your WordPress site through https://localhost:80
in your browser.
The volumes
section defines a volume that persists the wp-content
directory between container restarts. This ensures your uploads, themes, and plugins are preserved.
services:
# ... (WordPress service configuration)
db:
image: mysql:8.0 # Use the official MySQL image
environment:
environment:
MYSQL_ROOT_PASSWORD: shamsher123 # Set the MySQL root password
volumes:
- mysql-data:/var/lib/mysql # Persist database data (optional)
- This configuration uses the
mysql:8.0
image to create a MySQL container. - The
environment
section sets theMYSQL_ROOT_PASSWORD
environment variable with a placeholder valueshamsher123
. Remember to replace this with a strong password for security reasons. - The
volumes
section (optional) defines a volume namedmysql-data
that persists the database data directory (/var/lib/mysql
) between container restarts. This ensures your database information is not lost when the container stops.
6 Running the WordPress Application
With your docker-compose.yml file complete, you can start your WordPress application using the following command in your terminal:
docker-compose up -d
- The
docker-compose up
command instructs Docker Compose to build and start the services defined in your docker-compose.yml file. - The
-d
flag tells Docker Compose to run the containers in detached mode, meaning they’ll run in the background without needing your terminal open.
Once the command finishes, your WordPress application and database will be running in separate containers. You can access your WordPress admin panel through https://localhost:80/wp-admin
using the password you set for the MYSQL_ROOT_PASSWORD
environment variable.
Part 3: Conclusion and Next Steps
This article provided a basic introduction to docker-compose.yml and its role in managing a WordPress application with Docker Compose. You’ve learned how to:
- Define services (containers) for your application.
- Configure ports, volumes, and environment variables.
- Persist data between container restarts.
Customization and Next Steps
The docker-compose.yml file offers extensive customization options. Here are some possibilities:
- Persistent storage: Configure additional volumes to persist themes, plugins, or custom configurations.
- Custom themes/plugins: Map your local development directory with themes and plugins to the container for easier development.
- Environment variables: Define additional environment variables for WordPress configuration.
For in-depth exploration, refer to the official Docker documentation on docker-compose.yml (https://docs.docker.com/compose/compose-file/legacy-versions/) and WordPress Docker setups (https://www.youtube.com/watch?v=lDgK1WL3hJI).
This article has equipped you with the foundation for building and managing WordPress applications with Docker Compose. Feel free to experiment and customize your docker-compose.yaml file to suit your specific needs.