Docker PHP MySQL Setup
October 22, 2024 · AI Conversations
### Step 1: Create a `docker-compose.yml` file
In your project directory, create a file named `docker-compose.yml` and add the following content:
version: '3.8'
services:
# Apache and PHP 8 service
web:
image: php:8.0-apache
container_name: php_apache
volumes:
- ./src:/var/www/html
ports:
- "8080:80"
networks:
- app-network
depends_on:
- db
# MySQL service
db:
image: mysql:8.0
container_name: mysql_db
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: my_database
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
- db_data:/var/lib/mysql
ports:
- "3306:3306"
networks:
- app-network
# Adminer service
adminer:
image: adminer
container_name: adminer
depends_on:
- db
ports:
- "8081:8080"
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
db_data:
### Step 2: Directory Structure
Make sure to create the following structure in your project directory:
project-directory/
│
├── src/ # Place your PHP files here (e.g., index.php)
│
├── docker-compose.yml
### Step 3: Start the Docker Containers
Run the following command to start all the containers:
docker-compose up -d
– **PHP and Apache** will be accessible on `http://localhost:8080`.
– **Adminer** (a web-based MySQL management tool) will be available at `http://localhost:8081`.
– **MySQL** will be running on `localhost:3306`.
### Step 4: Accessing Services
– You can place your PHP files inside the `src` directory.
– Visit `http://localhost:8081` to access Adminer, and use the credentials defined in the `db` service to log in:
– System: `MySQL`
– Server: `db` (because Adminer and MySQL are in the same Docker network)
– Username: `user`
– Password: `password`
– Database: `my_database`
This should give you a complete local development environment for PHP 8, MySQL, Adminer, and Apache using Docker!