What You’ll Learn
This guide walks you through a practical docker-compose.yml
setup for launching a WordPress site with a MySQL database. You’ll understand key Docker Compose concepts while building a fully functional stack.
Prerequisites
- Install Docker Engine
- Install Docker Compose
- Basic terminal familiarity (Linux/macOS/WSL)
Configuration Breakdown
version: '3.8' # Compose file syntax version
services:
# MySQL Database Service
db:
image: mysql:8.0 # Official MySQL image
volumes:
- db_data:/var/lib/mysql # Persistent data storage
environment:
MYSQL_ROOT_PASSWORD: my-secret-pw # Never use in production!
MYSQL_DATABASE: wordpress
MYSQL_USER: wp_user
MYSQL_PASSWORD: wp_password
networks:
- wp-network
# WordPress Service
wordpress:
image: wordpress:php8.2-apache
depends_on:
- db # Start DB first
ports:
- "8000:80" # Host:Container port mapping
volumes:
- wp_data:/var/www/html # Theme/plugin persistence
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wp_user
WORDPRESS_DB_PASSWORD: wp_password
WORDPRESS_DB_NAME: wordpress
networks:
- wp-network
# Infrastructure Definitions
volumes:
db_data: # Named volume for database
wp_data: # Named volume for WordPress
networks:
wp-network: # Private network for secure inter-service communication
Key Concepts Explained
-
Services
db
: MySQL container with preconfigured database/userwordpress
: Apache/PHP container with WordPress
-
Volumes
db_data
: Preserves database files after container restartwp_data
: Saves WordPress themes/plugins/uploads
(Find volumes withdocker volume ls
)
-
Networking
- Private
wp-network
enables secure communication using service names (db
,wordpress
) as DNS hosts
- Private
-
Environment Variables
- Configure application settings (database credentials, ports)
- Production Tip: Use
.env
files instead of hardcoding values
Launch Your Stack
- Save configuration as
docker-compose.yml
- Start services:
docker-compose up -d
- Verify running containers:
docker-compose ps
Access WordPress at: http://localhost:8000
Management Commands
Command | Description |
---|---|
docker-compose stop |
Stop containers gracefully |
docker-compose down -v |
Destroy containers AND volumes |
docker-compose logs -f db |
View database logs |
docker-compose restart wordpress |
Restart WordPress service |
Security Recommendations
- Always replace default credentials
- Use secrets management for production:
environment: MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password secrets: - db_root_password
- Add resource limits to services:
wordpress: deploy: resources: limits: cpus: '1.0' memory: 512M
Next Steps
- Add
phpmyadmin
service for database management - Configure HTTPS with an Nginx reverse proxy
- Explore Docker Compose profiles for dev vs production setups
> Troubleshooting Tip: If WordPress shows “Error establishing database connection”, verify your db
service is running with docker-compose logs db
.
This setup demonstrates Docker Compose’s power for defining multi-container applications. Modify the environment variables and volume paths to match your specific requirements! 🐳