Building a PHP 8.4 + MySQL Docker Development Environment: Complete Guide

Author

Kritim Yantra

Apr 13, 2025

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:

  1. Base image: php:8.4-apache (PHP 8.4 with Apache)
  2. MySQL extension installation
  3. 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/html enables live code reloading
  • Persistent Storage: db_data volume preserves database between restarts
  • Dependency Management: depends_on ensures db starts first

4. Running the Application

  1. Build and start containers:

    docker-compose up --build
    
  2. Access 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
      
  3. Management commands:

    • Stop containers: docker-compose down
    • Stop with volume removal: docker-compose down -v
    • View logs: docker-compose logs -f

5. Development Workflow Enhancements

Live Development

  • Any changes to index.php are immediately reflected (thanks to volume mounting)
  • No need to rebuild or restart containers for code changes

Database Management

  1. Access MySQL CLI:

    docker-compose exec db mysql -u root -psecret testdb
    
  2. Persistent Data:

    • Database survives container restarts
    • Located in Docker volume db_data

Troubleshooting

  1. Connection Issues:

    • Verify MySQL is running: docker-compose ps
    • Check logs: docker-compose logs db
  2. Permission Problems:

    • On Linux, you might need to add :z to volume mounts:
      volumes:
        - .:/var/www/html:z
      

6. Production Considerations

For production deployments:

  1. Replace hardcoded credentials with environment variables
  2. Implement proper security (firewalls, limited privileges)
  3. Configure proper logging
  4. Set up backups for the database volume
  5. Consider using php-fpm with 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:

  1. Clone the repository
  2. Run docker-compose up --build
  3. Visit http://localhost:8000

This approach gives you a professional-grade development environment that mirrors production while being easy to manage. Happy coding! 🚀

Tags

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts