Contents
Introduction
In modern web development, containerization has revolutionized the way projects are built, tested, and deployed. Docker Compose is an orchestration tool that simplifies the definition and management of multi-container Docker applications via a single YAML file. When working with WordPress, Compose streamlines the setup of the web server, PHP runtime, database, and any additional services you require. This article provides an extensive guide to setting up a professional, maintainable, and scalable WordPress development environment using Docker Compose.
Why Use Docker Compose for WordPress Development
- Consistency: Eliminate the “it works on my machine” dilemma by ensuring every team member runs the same environment.
- Modularity: Separate containers for Nginx/Apache, PHP-FPM, MySQL/MariaDB, and caching layers (Redis, Varnish) increase flexibility.
- Isolation: Avoid version conflicts between projects by isolating dependencies.
- Reproducibility: Easily replicate environments for CI pipelines, testing, or staging with a single command.
- Extensibility: Add services such as MailHog for email testing, Adminer for database management, and Xdebug for debugging.
Prerequisites
- Docker Engine installed. Follow the official guide: https://docs.docker.com/engine/install/
- Docker Compose (or Docker Compose V2) installed: https://docs.docker.com/compose/install/
- Basic familiarity with the command line, YAML, and WordPress file structure.
- Optional: git for version control and Makefile for task automation.
Directory Structure
A well-organized project might look like this:
docker-compose.yml
– Main Compose file.env
– Environment variable definitionswp-content/
– Themes, plugins, uploadsmysql-data/
– Persisted database filesconfig/php.ini
– Custom PHP settingsconfig/nginx.conf
– Nginx server configurationMakefile
(optional) – Automate common tasks
Creating the docker-compose.yml
This example sets up WordPress with MariaDB and a basic Nginx reverse proxy:
services:
db:
image: mariadb:10.5
container_name: wp_db
volumes:
– ./mysql-data:/var/lib/mysql
env_file:
– .env
networks:
– wp-network
wordpress:
image: wordpress:latest
container_name: wp_app
depends_on:
– db
volumes:
– ./wp-content:/var/www/html/wp-content
– ./config/php.ini:/usr/local/etc/php/conf.d/custom.ini
env_file:
– .env
networks:
– wp-network
proxy:
image: nginx:alpine
container_name: wp_proxy
depends_on:
– wordpress
volumes:
– ./config/nginx.conf:/etc/nginx/conf.d/default.conf:ro
ports:
– 80:80
networks:
– wp-network
networks:
wp-network:
driver: bridge
Environment Variables Configuration
Store sensitive or variable settings in .env
(keep this file out of version control). Example:
WORDPRESS_DB_USER=wp_user
WORDPRESS_DB_PASSWORD=secure_password
WORDPRESS_DB_NAME=wordpress
MYSQL_ROOT_PASSWORD=root_password
Key variables explained:
Variable | Description |
---|---|
WORDPRESS_DB_HOST |
Hostname:port of the database service. |
WORDPRESS_DB_USER |
Username for WordPress DB connection. |
WORDPRESS_DB_PASSWORD |
Password for the database user. |
MYSQL_ROOT_PASSWORD |
Root password for MariaDB. |
Persistent Volumes and Data Management
- Database data: Mapped to
./mysql-data
to preserve data across container restarts. - wp-content: Host-mounted to easily develop themes/plugins with live reload.
- Use
docker volume
for more advanced volume management.
Networking and Service Discovery
All services share a user-defined bridge network (wp-network
), enabling internal DNS resolution. Containers can reference each other by service name (e.g., db
or wordpress
).
Managing Your Environment
- Start:
docker-compose up -d
- Stop:
docker-compose down
(use--volumes
to remove volumes) - Logs:
docker-compose logs -f
- Rebuild:
docker-compose up -d --build
- Execute a shell:
docker-compose exec wordpress bash
Troubleshooting and Tips
- Port conflicts: Ensure port 80 is free or map to a different host port.
- File permissions: Adjust UID/GID when mounting volumes to avoid permission errors.
- Database crashes: Check
mariadb
logs and validateMYSQL_ROOT_PASSWORD
. - Plugin/theme refresh: Use
docker-compose restart wordpress
or clear caches. - Network connectivity: Verify that services reside on the same network and names match.
Advanced Topics
Multisite Configuration
Enable WordPress Multisite by adding WORDPRESS_CONFIG_EXTRA
in .env
:
Adding Xdebug for PHP Debugging
- Extend the
wordpress
service with a custom Dockerfile:
RUN pecl install xdebug
docker-php-ext-enable xdebug
COPY config/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
Configure xdebug.ini
for remote_enable, client_host, and port.
Further Reading
Conclusion
Utilizing Docker Compose for WordPress development delivers a robust, repeatable, and flexible workflow. You gain the power to customize, scale, and isolate every component of your stack while maintaining simplicity in orchestration. By following best practices—structured configuration, environment management, and service modularity—you’ll accelerate development, minimize “works on my machine” issues, and pave the way for streamlined CI/CD integration.
|
Acepto donaciones de BAT's mediante el navegador Brave 🙂 |