A local development environment is crucial for PHP developers to test and debug their applications. Traditionally, developers would rely on tools like XAMPP, MAMP, or WAMP, which provide a full-fledged LAMP stack (Linux, Apache, MySQL, PHP) for local development. However, as applications grow more complex and need to mimic production environments accurately, Docker has become the go-to solution.
Docker allows developers to containerize their applications, ensuring that they run consistently across different environments. In this article, we will guide you through the process of setting up a PHP local development environment using Docker.
- Docker installed on your machine. If you haven’t installed Docker yet, visit the official Docker website for instructions.
- Basic knowledge of Docker and its command-line interface.
Step 1. Create a Dockerfile
The first step is to create a Dockerfile, which describes the environment for our PHP application.
FROM php:8.2-apache # Install required PHP extensions RUN docker-php-ext-install mysqli pdo pdo_mysql # Enable Apache mod_rewrite RUN a2enmod rewrite # Copy your application files to the container COPY ./src/ /var/www/html/
Step 2. Set Up Docker Compose
Docker Compose allows us to define and run multi-container Docker applications. This is useful when your PHP application relies on other services like MySQL.
Create a docker-compose.yml file:
version: '3' services: web: build: context: . dockerfile: Dockerfile ports: - "80:80" volumes: - ./src:/var/www/html db: image: mysql:8.0 environment: MYSQL_ROOT_PASSWORD: rootpassword MYSQL_DATABASE: mydatabase MYSQL_USER: user MYSQL_PASSWORD: password
Step 3. Build and Run
With your Dockerfile and docker-compose.yml in place, navigate to the directory containing these files in your terminal and run:
docker-compose up -d
This command will:
- Build the Docker image as per your Dockerfile.
- Start a container for PHP and Apache, with the code in the ./src directory being served.
- Start a MySQL container using the official MySQL 8.0 image.
Step 4. Accessing the Application
Once everything is up, you can access your PHP application by navigating to http://localhost in your browser. Similarly, you can connect to the MySQL database using the credentials specified in the docker-compose.yml file.
Step 5. Stopping and Removing Containers
To stop the running containers, use:
Advantages of Using Docker
- Isolation: Docker containers ensure that your application runs the same, irrespective of where Docker is running.
- Version Control for Environments: Docker allows you to specify the exact versions of PHP, Apache, and other services, ensuring consistency across all development environments.
- Integration with Modern Development Tools: Many CI/CD tools now support Docker, making it easier to push your application from development to production.
Setting up a PHP local development environment with Docker provides consistency, flexibility, and ease of use. By containerizing your application, you can ensure that it behaves the same way across different stages, reducing the “it works on my machine” problem. As you continue your Docker journey, you can explore more advanced topics like optimizing your Docker images, integrating with other tools, and using Docker Swarm or Kubernetes for orchestration.