Docker Tutorial

Docker Installation

How to use Docker

Docker Instance

Docker Container Lifecycle Command

Docker Container Operation Command

Docker Container rootfs Command

Docker Mirror Repository Command

Docker Local Image Management Command

Docker info|version Command

Docker install PHP

In this tutorial, you'll learn how to create a Docker container running PHP, a popular server-side scripting language widely used for web development. We'll use the official PHP image provided by the Docker community.

  1. Prerequisites:

    • Install Docker on your system
  2. Pull the official PHP image:

    To pull the official PHP image with Apache, run the following command:

    docker pull php:apache
    

    This will download the latest version of the PHP image with Apache web server to your system.

  3. Create a sample PHP application:

    First, create a new directory for your PHP application:

    mkdir php-app
    cd php-app
    

    Next, create a new file named index.php in the php-app directory with the following content:

    <?php
    echo 'Hello World!';
    ?>
    

    This is a simple PHP script that prints "Hello World!" when executed.

  4. Create a new container with PHP and Apache:

    To create a new Docker container running your PHP application with Apache, use the docker run command. We'll map the container's port 80 (the default Apache HTTP port) to the host's port 8080, so you can access the application from your host machine. Additionally, we'll mount the local php-app directory as a volume inside the container:

    docker run --name php-container -p 8080:80 -v $(pwd):/var/www/html -d php:apache
    

    This command creates a new container named php-container, maps the container's port 80 to the host's port 8080, mounts the local php-app directory as a volume at /var/www/html inside the container, and runs the container in detached mode (-d flag). The container will run the Apache web server with PHP enabled and serve the index.php file.

  5. Verify the PHP container is running:

    To check if the PHP container is running, use the docker ps command:

    docker ps
    

    You should see your php-container listed as running.

  6. Access the PHP application:

    Open your browser and navigate to http://localhost:8080. You should see the "Hello World!" message from your PHP application.

In this tutorial, we covered how to create a Docker container running a simple PHP application with the Apache web server, access the application from your host machine, and update the application while it's running. You can now use this containerized PHP environment for development, testing, or deployment purposes.

  1. Running PHP in Docker Container:

    • Description: Run a PHP application in a Docker container for a consistent and isolated execution environment.
    • Code Example:
      docker run -d -p 80:80 --name my-php-container php:latest-apache
      
  2. How to Create a Docker Image with PHP:

    • Description: Create a custom Docker image with PHP installed using a Dockerfile.
    • Code Example (Dockerfile):
      FROM php:latest-apache
      # Add custom configurations or install extensions
      
  3. Docker-Compose PHP Installation:

    • Description: Use Docker Compose to define and run multi-container Docker applications, including PHP.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        php:
          image: php:latest-apache
          ports:
            - "80:80"
      
  4. PHP Dockerfile Example:

    • Description: A Dockerfile example for creating a custom PHP image with additional configurations or application files.
    • Code Example (Dockerfile):
      FROM php:latest-apache
      COPY php.ini /usr/local/etc/php/php.ini
      
  5. Setting Up PHP in a Docker Environment:

    • Description: Configure PHP settings, such as PHP.ini or document root, within a Docker environment.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        php:
          image: php:latest-apache
          ports:
            - "80:80"
          volumes:
            - ./php.ini:/usr/local/etc/php/php.ini
      
  6. Configuring PHP in Docker-Compose:

    • Description: Customize PHP configurations using Docker Compose for a multi-container environment.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        php:
          image: php:latest-apache
          ports:
            - "80:80"
          volumes:
            - ./php.ini:/usr/local/etc/php/php.ini
            - ./htdocs:/var/www/html
      
  7. Docker PHP Image Customization:

    • Description: Customize the PHP image by adding configurations, extensions, or specific application files.
    • Code Example (Dockerfile):
      FROM php:latest-apache
      COPY php.ini /usr/local/etc/php/php.ini
      
  8. Docker PHP Volume Mounts:

    • Description: Use Docker volumes to mount external directories or PHP configuration files into PHP containers for persistence.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        php:
          image: php:latest-apache
          volumes:
            - ./php.ini:/usr/local/etc/php/php.ini
            - ./htdocs:/var/www/html
      
  9. Custom PHP Image Creation in Docker:

    • Description: Create a custom PHP image with specific configurations and dependencies tailored to your application.
    • Code Example (Dockerfile):
      FROM php:latest-apache
      COPY php.ini /usr/local/etc/php/php.ini
      
  10. Docker Swarm and PHP Service:

    • Description: Deploy PHP as a service in a Docker Swarm to scale and manage multiple PHP containers.
    • Code Example (docker-compose.yml):
      version: '3'
      services:
        php:
          image: php:latest-apache
          deploy:
            replicas: 3
            # Other Swarm deployment configurations
      
  11. Troubleshooting PHP in Docker:

    • Description: Troubleshoot PHP-related issues in Docker by examining logs, checking configurations, and verifying container settings.
    • Code Example:
      docker logs <php_container_id_or_name>