Building a PHP 8.4 + MySQL Docker Development Environment: Complete Guide
Introduction
In this comprehensive guide, we'll create a complete PHP 8.4 and MySQL development environment using Docker. This setup includes:
- PHP 8.4 with Apache
- MySQL 8.0 database
- Persistent database storage
- Real-time code synchronization using volumes
Project Structure
php-mysql-docker/
│
├── index.php # PHP application code
├── Dockerfile # PHP/Apache container configuration
└── docker-compose.yml # Orchestration for both services
1. ✅ The PHP Application (index.php)
<?php
$servername = "db"; // name of the mysql container
$username = "root";
$password = "secret";
$database = "testdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully!";
Security Note: In production, always:
- Use environment variables for credentials
- Implement prepared statements
- Add proper error handling
2. ✅ Dockerfile Configuration
Our Dockerfile builds a custom PHP 8.4 + Apache image:
FROM php:8.4-apache
# Install MySQL extension
RUN docker-php-ext-install mysqli
# Copy our PHP file
COPY index.php /var/www/html/
# Recommended: Enable Apache rewrite module
RUN a2enmod rewrite
Key Components:
- Base image:
php:8.4-apache(PHP 8.4 with Apache) - MySQL extension installation
- File copying to web root
3. ✅Docker Compose Setup
The docker-compose.yml defines our multi-container application:
version: '3.8'
services:
web:
build: .
ports:
- "8000:80"
volumes:
- .:/var/www/html
depends_on:
- db
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: testdb
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
Configuration Breakdown:
| Service | Purpose | Key Configurations |
|---|---|---|
| web | PHP/Apache | Port mapping, volume mount, build context |
| db | MySQL 8.0 | Root password, initial database, persistent volume |
Important Features:
- Volume Mounting:
.:/var/www/htmlenables live code reloading - Persistent Storage:
db_datavolume preserves database between restarts - Dependency Management:
depends_onensures db starts first
4. Running the Application
Build and start containers:
docker-compose up --buildAccess the application:
- Open: http://localhost:8000
- Expected output:
Connected successfully! Table 'products' checked/created successfully. Dummy data inserted successfully. Product List: - Product A - $10.99 - Product B - $20.50 - Product C - $5.75
Management commands:
- Stop containers:
docker-compose down - Stop with volume removal:
docker-compose down -v - View logs:
docker-compose logs -f
- Stop containers:
5. Development Workflow Enhancements
Live Development
- Any changes to
index.phpare immediately reflected (thanks to volume mounting) - No need to rebuild or restart containers for code changes
Database Management
Access MySQL CLI:
docker-compose exec db mysql -u root -psecret testdbPersistent Data:
- Database survives container restarts
- Located in Docker volume
db_data
Troubleshooting
Connection Issues:
- Verify MySQL is running:
docker-compose ps - Check logs:
docker-compose logs db
- Verify MySQL is running:
Permission Problems:
- On Linux, you might need to add
:zto volume mounts:volumes: - .:/var/www/html:z
- On Linux, you might need to add
6. Production Considerations
For production deployments:
- Replace hardcoded credentials with environment variables
- Implement proper security (firewalls, limited privileges)
- Configure proper logging
- Set up backups for the database volume
- Consider using
php-fpmwith Nginx for better performance
Conclusion
This setup provides a perfect local development environment with:
✅ PHP 8.4 + Apache
✅ MySQL 8.0 with persistence
✅ Real-time code synchronization
✅ Automatic database initialization
To get started:
- Clone the repository
- Run
docker-compose up --build - Visit
http://localhost:8000
This approach gives you a professional-grade development environment that mirrors production while being easy to manage. Happy coding! 🚀
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Flask Blog CMS with Admin Dashboard — Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.