Prepare the mirror



I use centos system, the latest version of Docker-CE.



Prepare three mirror images



Docker pull mysql: 5.7



Docker pull PHP: 5.6 – FPM



docker pull nginx:latest



Note, do not use the latest version of mysql and PHP, there are many bugs.







1

Docker images # View images





Build a mysql container



Docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=ziqin666 -v /mysql:/var/lib/mysql –name your_mysql mysql:5.7

Configuring containers (entering containers)



docker exec -it your_mysql bash

Once inside, authorize the user to use the remote connection



Enter the password ziqin666 to connect to mysql



GRANT ALL PRIVILEGES ON *.* TO ‘root’@’localhost’ IDENTIFIED BY ‘ziqin666’;

GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’ IDENTIFIED BY ‘ziqin666’;

GRANT ALL PRIVILEGES ON *.* TO ‘root’@’127.0.0.1’ IDENTIFIED BY ‘ziqin666’;

FLUSH PRIVILEGES;



At this point you can use the client to connect to see. (Ensure that the port corresponding to the security group rule is enabled.)







Build the phP-FPM container



The preparatory work



Php.ini does not exist by default



Create the php.ini file and conf.d folder in the /home/app/phpfile folder



Create a container



1

docker run -p 9000:9000 –name your_phpfpm -v /home/app/html:/var/www/html -v /home/app/phpfile:/usr/local/etc/php – link your_mysql: PHP mysql – d: 5.6 – FPM

Command description:



–name your_phpfpm: Name the container your_phpfpm.

– v/home/app/HTML: / var/WWW/HTML: the host project directory in/home/app/HTML mounted to the container/var/WWW/HTML





Install pDO to test the database connection later, which can be done in the DOCker PHP container



docker-php-ext-install pdo_mysql





Installing other plug-ins can also use this approach



Add to php.ini



extension=php_curl.dll

extension=php_gd2.dll

extension=php_mysql.dll

extension=php_mysqli.dll

extension=php_pdo_mysql.dll

extension=php_pdo_odbc.dll

extension=php_pdo.dll

If plug-ins are not enough, you need to customize your own image



The FROM PHP: 7.0.12 – FPM

MAINTAINER Tairy <[email protected]>



WORKDIR /working

RUN apt-get update –fix-missing && apt-get install -y \

g++ autoconf bash git apt-utils libxml2-dev libcurl3-dev pkg-config \

&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \

&& echo “Asia/Shanghai” > /etc/timezone \

&& docker-php-ext-install iconv curl mbstring \

xml json mcrypt mysqli pdo pdo_mysql zip \

&& docker-php-ext-configure gd \

–with-gd \

–with-freetype-dir=/usr/include/ \

–with-png-dir=/usr/include/ \

–with-jpeg-dir=/usr/include/ \

&& docker-php-ext-install gd \

&& docker-php-ext-enable gd \

Pecl install /pecl/redis-3.0.0.tgz \

&& docker-php-ext-enable redis \

&& apt-get purge -y –auto-remove \

&& rm -rf /var/cache/apt/* \

&& rm -rf /var/lib/apt/lists/* \

&& rm -rf /pecl

Install nginx



/home/app/nginx/conf.d



Create default.conf in the configuration file directory



server

{

listen 80;

server_name localhost;



location / {

root /usr/share/nginx/html;

index index.html index.htm;

}

location ~ \.php$ {

fastcgi_pass your_phpfpm:9000; Docker PHP name: your_phpfpm

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;

include fastcgi_params;

}



error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/html;

}

}

Create a container



docker run -p 81:80 –name your_nginx \

-v /home/app/nginx/www:/usr/share/nginx/html \

-v /home/app/nginx/conf.d:/etc/nginx/conf.d \

–link your_phpfpm:phpfpm \

-d nginx:latest





PHPFPM and mysql in the mapping container are fixed and cannot be modified!



/home/app/nginx/ WWW -data: www_data/WWW -data: www_data/WWW -data: www_data



chown -R www-data:www-data www

Four, test,



1. View services



docker ps -a

STATUS is up, that is, running



2. Test PHP parsing



Change the index. PHP file in /home/app/nginx/ WWW/on the host.



<? php echo phpinfo();



3, test the mysql link



Modified index. PHP



<? php

//echo phpinfo();

$dbms=’mysql’; // Database type

$host=’your_mysql’; // Database host name, where write mysql container name

$dbport = ‘3306’;

$dbName=’mysql’; // The database used

$user=’root’; // Database connection user name

$pass=’123456′; // The corresponding password

$dsn=”$dbms:host=$host; port=$dbport; dbname=$dbName”;



try {

$dbh = new PDO($dsn, $user, $pass); // Initialize a PDO object

echo “successful<br/>”;

You can also perform a search operation

// foreach ($dbh->query(‘SELECT * from user’) as $row) {

// print_r($row); Echo ($GLOBAL); To see these values

// }



$dbh = null;

} catch (PDOException $e) {

die (“Error! : ” . $e->getMessage() . “<br/>”);

}

Accessing the IP to the correct output proves that our configuration is successful.