Docker Install PHP

Docker Install PHP

Installing the PHP image

docker pull php

Find the php image on Docker Hub:

Docker Install PHP

You can check other versions of php by Sort by, the default is the latest version php:latest.

In addition, we can use the docker search php command to see what versions are available.

apidemos@apidemos:~/php-fpm$ docker search php
NAME                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
php                       While designed for web development, the PH...   1232      [OK]       
richarvey/nginx-php-fpm   Container running Nginx + PHP-FPM capable ...   207                  [OK]
phpmyadmin/phpmyadmin     A web interface for MySQL and MariaDB.          123                  [OK]
eboraas/apache-php        PHP5 on Apache (with SSL support), built o...   69                   [OK]
php-zendserver            Zend Server - the integrated PHP applicati...   69        [OK]       
million12/nginx-php       Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS...   67                   [OK]
webdevops/php-nginx       Nginx with PHP-FPM                              39                   [OK]
webdevops/php-apache      Apache with PHP-FPM (based on webdevops/php)    14                   [OK]
phpunit/phpunit           PHPUnit is a programmer-oriented testing f...   14                   [OK]
tetraweb/php              PHP 5.3, 5.4, 5.5, 5.6, 7.0 for CI and run...   12                   [OK]
webdevops/php             PHP (FPM and CLI) service container             10                   [OK]
...

Here we pull the official image, labeled 5.6-fpm

apidemos@apidemos:~/php-fpm$ docker pull php:5.6-fpm

Once the download is complete, we can check the local mirror list for the REPOSITORY of php, labeled 5.6-fpm.

apidemos@apidemos:~/php-fpm$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
php                 5.6-fpm             025041cd3aa5        6 days ago          456.3 MB

Nginx + PHP Deployment

Start PHP.

$ docker run --name  myphp-fpm -v ~/nginx/www:/www  -d php:5.6-fpm

Command description.

  • –name myphp-fpm : Names the container as myphp-fpm.
  • -v ~/nginx/www:/www : Mount the directory www of the project in the host to /www of the container

Create ~/nginx/conf/conf.d directory.

mkdir ~/nginx/conf/conf.d 

Add the ~/nginx/conf/conf.d/apidemos-test-php.conf file to that directory, with the following contents.

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php{
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/fastcgi_script_name;
        include        fastcgi_params;
    }
}

Configuration file description.

  • php:9000: represents the URL of the php-fpm service, which we will specify below.
  • /www/: is the path to store php files in myphp-fpm, mapped to the local ~/nginx/www directory.

To start nginx.

docker run --name apidemos-php-nginx -p 8083:80 -d \
    -v ~/nginx/www:/usr/share/nginx/html:ro \
    -v ~/nginx/conf/conf.d:/etc/nginx/conf.d:ro \
    --link myphp-fpm:php \
    nginx
  • -p 8083:80: port mapping, mapping 80 in nginx to local port 8083.
  • ~/nginx/www: is the local html file storage directory, /usr/share/nginx/html is the container html file storage directory.
  • ~/nginx/conf/conf.d: is the directory where local nginx configuration files are stored, and /etc/nginx/conf.d is the directory where nginx configuration files are stored inside the container.
  • –link myphp-fpm:php: Merges the network of myphp-fpm into nginx\ and maps the domain php to 127.0.0.1 by modifying /etc/hosts in nginx, allowing nginx to access php-fpm via php:9000. fpm.

Next, we create index.php in the ~/nginx/www directory with the following code.

<?php
echo phpinfo();
?>

The browser opens http://127.0.0.1:8083/index.php and displays the following.

Docker Install PHP

Like(0)