Here are the steps to set up a LAMP development environment with Docker on a Mac:
Install Docker Desktop on your Mac if you haven’t already. You can download it from the official Docker website: https://www.docker.com/products/docker-desktop
Create a new directory for your project on your local machine. In this example, we’ll call it “asvignesh”.
Create a new file named “docker-compose.yml” inside the “asvignesh” directory, and add the following content:
version: '3'
services:
web:
image: php:7.4-apache
ports:
- "80:80"
volumes:
- ./src:/var/www/html/
depends_on:
- db
db:
image: mysql:8
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: vignesh
MYSQL_DATABASE: vignesh
MYSQL_USER: vignesh
MYSQL_PASSWORD: vignesh
volumes:
- ./mysql:/var/lib/mysql
This file specifies two Docker services: “web” and “db”.
The “web” service uses the official PHP 7.4 Apache image and maps port 80 on the Docker container to port 80 on the host machine. It also mounts the “src” directory inside the Docker container at “/var/www/html/”, which allows you to edit your PHP files on your local machine and see the changes immediately on the Docker container. The “db” service uses the official MySQL 8 image and maps port 3306 on the Docker container to port 3306 on the host machine. It also sets some environment variables to configure the MySQL server and mounts the “mysql” directory inside the Docker container at “/var/lib/mysql”, which allows you to persist the database data between Docker container restarts.
Create a new directory named “src” inside the “asvignesh” directory and add your PHP files inside it.
For testing i create index.php with the below code
<?php
// Show all information, defaults to INFO_ALL
phpinfo();
// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);
?>
Open a terminal window and navigate to the “asvignesh” directory.
Run the following command to start the Docker containers:
docker-compose up -d
This command downloads the necessary Docker images (if they are not already downloaded) and starts the “web” and “db” services in the background.
Open a web browser and go to http://localhost to access your PHP application running inside the Docker container.
That’s it! You now have a LAMP development environment running inside Docker on your Mac. You can edit your PHP files on your local machine and see the changes immediately on the Docker container, and you can use the MySQL server running inside the Docker container to store your data.
Also published on Medium.