Docker deployed separately LNMP

First, prepare the environment

Installation on docker can refer Docker Introduction and installation configuration in detail here do not introduced!

Case requirements:
(1) the respective container needs data persistence;
(2) assign a fixed IP address to a container, the container prevent the reconstruction, the IP address is changed, unnecessary trouble;

Case environment:
can be installed on a docker host! Preset environment Figure:
Docker deployed separately LNMP

Second, the implementation of case

(1) create a network card, to resolve a fixed IP address

[root@docker ~]# docker network create -d bridge --subnet 200.0.0.0/24 --gateway 200.0.0.1 lnmp
//创建网卡lnmp,指定网段200.0.0.0/24

(2) to solve the problem directory nginx container

[root@docker ~]# docker run -itd --name test nginx        
//随便运行一个容器,以便生成nginx中所需的配置文件
[root@docker ~]# mkdir /data /wwwroot
//创建目录用于挂载nginx容器的相关目录
[root@docker ~]# docker cp test:/etc/nginx /data
//将nginx容器中的nginx主目录复制到本地
[root@docker ~]# docker cp test:/usr/share/nginx/html /wwwroot
//将nginx容器中的网页根目录复制到本地

(3) Run the test vessel Nginx

[root@docker ~]# docker run -itd --name nginx -v /data/nginx:/etc/nginx -v/wwwroot/html:/usr/share/nginx/html -p 80:80 --network lnmp --ip 200.0.0.10 nginx:latest
//运行一个名为nginx的容器,挂载相应的目录,并映射端口、指定IP地址
[root@docker ~]# echo "hello world" > /wwwroot/html/index.html 
[root@docker ~]# curl 127.0.0.1
hello world
//测试nginx网页根目录没有问题

(4) a container run mysql

[root@docker ~]# docker run -itd --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 --network lnmp --ip 200.0.0.20 mysql:5.7
//运行一个名为mysql的容器,并指定映射端口及IP地址
//使用“-e”选项来配置容器中的环境变量,相当于dockerfile中的ENV
[root@docker ~]# yum -y install mysql            //本地下载mysql客户端工具进行测试
[root@docker ~]# mysql -u root -p123456 -h 127.0.0.1 -P 3306
MySQL [(none)]> create database lzj;
//登录到数据库并创建数据库进行测试

(5) Run PHP container

[root@docker ~]# docker run -itd --name phpfpm -p 9000:9000 -v /wwwroot/html:/usr/share/nginx/html --network lnmp --ip 200.0.0.30 php:7.2-fpm
//运行一个名为phpfpm的容器,并映射端口,挂载相应的目录,指定IP地址
//看挂载的目录跟nginx主目录一样,是因为需要修改nginx容器的配置文件
[root@docker ~]# echo "<?php" >> /wwwroot/html/test.php
[root@docker ~]# echo "phpinfo();" >> /wwwroot/html/test.php
[root@docker ~]# echo "?>" >> /wwwroot/html/test.php 
//创建nginx与php连接测试的页面

(6) Nginx profile editing, and so can be connected Nginx php

[root@docker ~]# vim /data/nginx/conf.d/default.conf 
  8     location / {
  9         root   /usr/share/nginx/html;
 10         index  index.php index.html index.htm;     //添加index.php
 11     }
 30     location ~ \.php$ { 
 31         root           /usr/share/nginx/html;             //修改其网页根目录位置 
 32         fastcgi_pass   200.0.0.30:9000;                //更改为PHP容器的IP地址 
 33         fastcgi_index  index.php;
 34         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        //添加$document_root变量
 35         include        fastcgi_params; 
 36     }
 [root@docker ~]# docker restart nginx              //重新启动nginx容器
 [root@docker ~]# docker ps | grep nginx          //确保nginx容器是正常运行的
e000ccd5c883        nginx:latest        "nginx -g 'daemon of…"   27 minutes ago      Up 31 seconds       0.0.0.0:80->80/tcp                  nginx

Access test:
Docker deployed separately LNMP
Docker deployed separately LNMP

(7) the coordination of testing PHP and Mysql container vessel

We use this phpMyadmin database management tools.

Download phpMyadmin:

[root@docker ~]# wget https://files.phpmyadmin.net/phpMyAdmin/4.9.1/phpMyAdmin-4.9.1-all-languages.zip
[root@docker ~]# unzip phpMyAdmin-4.9.1-all-languages.zip 
[root@docker ~]# mv phpMyAdmin-4.9.1-all-languages /wwwroot/html/phpmyadmin
//将其移动到网页根目录下,并进行改名
[root@docker ~]# vim /data/nginx/conf.d/default.conf
……………………           //省略部分内容,添加以下内容
    location /phpmyadmin {
        root            /usr/share/nginx/html;
        index           index.html index.htm index.php;
}   
    location ~ /phpmyadmin/(?<after_ali>(.*)\.(php|php5)?$) {
        root           /usr/share/nginx/html;
        fastcgi_pass   200.0.0.30:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }   
[root@docker ~]# docker restart nginx           //重新启动nginx容器
[root@docker ~]# docker ps | grep nginx       确保nginx容器正常运行
e000ccd5c883        nginx:latest        "nginx -g 'daemon of…"   About an hour ago   Up 9 seconds        0.0.0.0:80->80/tcp                  nginx

Access test:
Docker deployed separately LNMP

Error because in normal compile and install PHP, you need to add "--with-mysql ..." and some other related options, see this page, it is clear that we run the PHP container did not increase the options for the database.

(8) to solve the associated container does not support PHP Mysql database

Login Docker's official website , as shown:
Docker deployed separately LNMP
Docker deployed separately LNMP
Now that you've seen the answer in Docker official website, took years to prepare Dockerfile php based on the original image to generate a new image to support this feature!

[root@docker ~]# vim Dockerfile
FROM php:7.2-fpm
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
    && docker-php-ext-install -j$(nproc) iconv \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \           
    && docker-php-ext-install mysqli pdo pdo_mysql    
//添加这行内容,使其支持连接mysql的功能
[root@docker ~]# docker build -t phpmysql .
//根据Dockerfile文件生成新的镜像
[root@docker ~]# docker rm phpfpm -f
//将原本的phpfpm容器进行删除
[root@docker ~]# docker run -itd --name phpfpm -p 9000:9000 -v /wwwroot/html:/usr/share/nginx/html --network lnmp --ip 200.0.0.30 phpmysql
//还是原本创建phpfpm容器的命令,只是将镜像更换为刚才制作的镜像了
[root@docker ~]# cd /wwwroot/html/phpmyadmin/
[root@docker phpmyadmin]# mv config.sample.inc.php config.inc.php 
[root@docker phpmyadmin]# vim config.inc.php           //编写php的配置文件 
 31 $cfg['Servers'][$i]['host'] = '200.0.0.20';
//更改其为mysql容器的IP地址
[root@docker ~]# docker restart phpfpm 
//重新启动php容器
[root@docker ~]# docker ps | grep phpfpm            //确保php容器运行正常
367e73bc70bd        phpmysql            "docker-php-entrypoi…"   6 minutes ago       Up 10 seconds       0.0.0.0:9000->9000/tcp              phpfpm

Access test:
Docker deployed separately LNMP
Docker deployed separately LNMP

------ this post. Thank reading ------------

Guess you like

Origin blog.51cto.com/14157628/2460505