Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»Setting Up Development Environments with PHP and Docker

    Setting Up Development Environments with PHP and Docker

    By RahulSeptember 17, 20233 Mins Read

    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.

    Prerequisites

    • 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:

    docker-compose down 
    

    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.

    Conclusion

    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.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Install and Use Podman on Ubuntu 22.04 & 20.04

    Setting Up Laravel with Docker and Docker-compose

    Using Composer with Different PHP Versions in Linux

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Create and Use Custom Python Module
    • How to Install and Use Podman on Ubuntu 22.04 & 20.04
    • Setting Up Laravel with Docker and Docker-compose
    • Setting Up Development Environments with PHP and Docker
    • Using Composer with Different PHP Versions in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.