Creating a Docker Compose configuration for MySQL, PHP 8, and Apache
Creating a Docker Compose configuration for MySQL, PHP 8, and Apache
1. Create a docker-compose.yml file:
version: '3.8' # Specify a compatible Docker Compose version
services:
db:
image: mysql:5.7 # Choose your preferred MySQL version
restart: always
environment:
MYSQL_ROOT_PASSWORD: your_root_password
MYSQL_DATABASE: your_database_name
volumes:
- db_data:/var/lib/mysql
php:
build: ./php # Build a custom PHP image from a Dockerfile
restart: always
volumes:
- ./src:/var/www/html # Mount your project directory
web:
image: httpd:2.4 # Use Apache as the web server
restart: always
ports:
- "80:80"
volumes:
- ./src:/var/www/html
volumes:
db_data: # Volume for persistent MySQL data
Here's a breakdown of the YAML configuration you provided:
Top-Level:
- version: '3.8': Specifies the Docker Compose file format version to use.
Services:
db:
image: mysql:5.7: Uses the MySQL 5.7 image for the database container.
restart: always: Automatically restarts the container if it fails.
environment: Sets environment variables for the container:
MYSQL_ROOT_PASSWORD: Sets the root password for MySQL.
MYSQL_DATABASE: Creates a database with this name on startup.
volumes: Mounts a persistent volume to store MySQL data:
- - db_data:/var/lib/mysql: Maps the named volume
db_data
to the MySQL data directory.
- - db_data:/var/lib/mysql: Maps the named volume
php:
build: ./php: Builds a custom PHP image from a Dockerfile in the
./php
directory.restart: always: Automatically restarts the container if it fails.
volumes: Mounts the project directory into the container:
- - ./src:/var/www/html: Maps the local
./src
directory to the web root within the container.
- - ./src:/var/www/html: Maps the local
web:
image: httpd:2.4: Uses the Apache 2.4 image for the web server container.
restart: always: Automatically restarts the container if it fails.
ports: Exposes ports:
- - "80:80": Maps port 80 of the host machine to port 80 of the container.
volumes: Mounts the project directory into the container (same as for PHP).
Volumes:
- db_data: Declares a named volume to persist MySQL data across container restarts.
Key Points:
This configuration defines three services: a MySQL database, a PHP application, and an Apache web server.
The services are interconnected: the PHP application interacts with the MySQL database, and the Apache web server serves the PHP application's files.
The
volumes
configuration ensures that data is persisted across container restarts and that project files are accessible within the containers.The
ports
configuration allows you to access the web application from your host machine.
2. Create a Dockerfile for the PHP image (in a ./php directory):
FROM php:8.0-apache # Base image with PHP 8 and Apache
RUN apt-get update && apt-get install -y \
libapache2-mod-php \
mysql-client \
# Add any other required PHP extensions
COPY src/ /var/www/html # Copy your project files
3. Build and run the containers:
docker-compose up -d
4. Access your application:
Open http://localhost in your browser to view your application.
Use
docker-compose exec php bash
to access the PHP container's shell.Use
docker-compose exec db mysql -uroot -p
to access the MySQL database.
Additional tips:
Customize the configuration (ports, passwords, etc.) as needed.
Consider using environment variables for sensitive information.
Explore additional Docker Compose features for networking, scaling, and more.
Install any additional PHP extensions required by your application.
Troubleshooting:
Check container logs for errors using
docker-compose logs
.Use
docker-compose down
to stop and remove the containers.Refer to the Docker Compose documentation for more advanced usage.