Home > Backend Development > PHP Tutorial > Docker for PHP begginers as simple as possible

Docker for PHP begginers as simple as possible

Linda Hamilton
Release: 2025-01-09 18:04:41
Original
141 people have browsed it

This tutorial guides beginners on setting up a simple PHP development environment using Docker. While I'm using Windows with WSL2, the process is largely the same for macOS and Linux. This approach prioritizes simplicity; advanced configurations are omitted.

Prerequisites:

Docker must be installed. Instructions vary by operating system but are generally straightforward. For Windows users, WSL2 is highly recommended (see Microsoft's documentation on installing Linux on Windows with WSL).

Steps:

  1. Start Docker: Ensure the Docker Desktop application is running. The running status is usually indicated by an icon in your system tray. Docker for PHP begginers as simple as possible

  2. Open Your IDE: Launch your preferred PHP IDE or editor (I use IntelliJ PHPStorm).

  3. Create a Project: Create a new project within your Ubuntu WSL2 environment. (Example path: \wsl.localhost\Ubuntu\home\development\docker-php-simple). Docker for PHP begginers as simple as possible

  4. Project Structure: Create the following directory structure and files within your project: Docker for PHP begginers as simple as possible

  5. File Content: Add the following code to the respective files:

    • public/index.php:

      <code class="language-php"><?php echo 'Hello, Developer!'; ?></code>
      Copy after login
    • .docker/apache/sites-available/000-default.conf:

      <code class="language-apache"><VirtualHost *:80>
          DocumentRoot "/var/www/html/public"
      </VirtualHost></code>
      Copy after login
    • docker-compose.yml:

      <code class="language-yaml">version: "3.9"
      services:
        app:
          image: php:8.4-apache
          container_name: docker-php-simple
          tty: true
          volumes:
            - ./:/var/www/html
            - ./.docker/apache/sites-available/000-default.conf:/etc/apache2/sites-available/000-default.conf
          ports:
            - "8080:80"</code>
      Copy after login

Running the Application:

Choose one of the following methods:

Option 1: Using Your IDE's Docker Plugin

  1. Open docker-compose.yml in your IDE.
  2. Locate the Docker Compose execution button (often represented by double arrows ▶︎▶︎ in PHPStorm). Click it to start the containers. The process and output will be visible in your IDE's console. Docker for PHP begginers as simple as possible

Option 2: Using the Command Line

  1. Open your terminal.
  2. Navigate to your project directory.
  3. Run: docker-compose up -d

After the containers start, your application should be accessible.

Verification:

Check the Docker application; you should see a running container named docker-php-simple. The container's status should be "Running." You can manage it (stop, restart, view logs) from the Docker dashboard. Docker for PHP begginers as simple as possible

Access your application in your browser at http://localhost:8080/. You should see "Hello, Developer!" displayed.

The above is the detailed content of Docker for PHP begginers as simple as possible. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template