Customizing LNMP environment Images with Dockerfile

Source: Internet
Author: User
Tags mysql client docker run

Lnmp is another very popular web framework behind the lamp, the linux+nginx+mysql+php site architecture scheme. Nginx is more lightweight than Apache, especially for static page processing. A friend of operations should know a popular word-dynamic static separation, one of the more common is to use nginx processing static pages, and dynamic pages to the backend Apache or tomcat processing. This article is to talk about the construction of LNMP environment image through Dockerfile, respectively, Nginx, MySQL and PHP deployed in the Docker container, the implementation of LNMP site architecture.

I. Introduction of the Project environment

1. System environment
Operating system version: Centos 7.5 64-bit
Docker version: 18.06.1-ce (Community Edition)
Nginx Version: 1.15.5
PHP Version: 7.2.11
MySQL version: 8.0.12
IP Address: 192.168.2.236
LNMP Network IP Address: 172.18.0.1
2. SOURCE Package
Nginx:http://nginx.org/download/nginx-1.15.5.tar.gz
Php:http://cn2.php.net/get/php-7.2.11.tar.gz/from/this/mirror
MySQL uses the official 8.0.12 image
3. Directory structure

[[email protected] ~]# tree dockerfile/dockerfile/├── nginx│?? ├── Dockerfile│?? ├── nginx-1.15.5.tar.gz│?? └── nginx.conf└── php    ├── Dockerfile    └── php-7.2.11.tar.gz
II. installation and deployment of the Docker environment

The installation of the Docker environment can be referenced in one of my other posts, "Centos 7 Deployment Docker environment, Basic command use, and simple combat", http://blog.51cto.com/andyxu/2174652 all the operations in this article are based on this environment.

Third, build PHP image

(1) Dockerfile file contents

From Centos:latestmaintainer Http://blog.51cto.com/andyxuENV time_zome asia/shanghaiarg pv= "php-7.2.11" ADD $ Pv.tar.gz/tmprun yum-y install gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl- Devel Bison && mkdir/data && cd/tmp/$PV &&/configure--prefix=/data/php--         With-config-file-path=/data/php/etc--with-gd--with-mysqli--with-openssl--with-zlib--with-curl     --with-jpeg-dir--with-png-dir--with-iconv--enable-fpm--enable-zip--enable-mbstring && make-j 4  && make install && cp/data/php/etc/php-fpm.conf.default/data/php/etc/php-fpm.conf && cp/data/php/etc/php-fpm.d/www.conf.default/data/php/etc/php-fpm.d/www.conf && sed-i '/;d Aemonize/a\daemon ize = No '/data/php/etc/php-fpm.conf && sed-i ' s/127.0.0.1/0.0.0.0/g '/data/php/etc/php-fpm.d/www.conf &A mp;& echo "${time_zome}" >/etc/TimeZone && Ln-sf/usr/share/zoneinfo/${time_zome}/etc/localtime && rm-rf/tmp/php* &&A mp Yum Clean all && yum-y remove gcc gcc-c++ makeworkdir/data/php/expose 9000CMD ["SBIN/PHP-FPM", "-C", "etc/php- Fpm.conf "]

Note: Daemonize = no means fpm is running in the foreground
(2) Build PHP image

[[email protected] ~]# cd dockerfile/php/[[email protected] php]# docker build -t php:7.2.11 .

The CentOS image is downloaded first, and then the new image is built, and it takes a long time to wait for a cup of tea.
(3) How to solve the error problem
If the Yum statement fails to execute, the following warning is reported:
[Warning] IPV4 forwarding is disabled. Networking won't work.
This problem is due to IPv4 forwarding is disabled, resulting in the inability to surf the internet, need to turn IPv4 forwarding on
Workaround:
vim /usr/lib/sysctl.d/00-system.conf
Add the following content
net.ipv4.ip_forward=1
Restart Network Service
systemctl restart network

Iv. Construction of Nginx image

(1) Dockerfile file contents

FROM centos:latestMAINTAINER http://blog.51cto.com/andyxuENV TIME_ZOME Asia/ShanghaiARG NV="nginx-1.15.5"COPY nginx.conf /data/nginx/conf/ADD $NV.tar.gz /tmpRUN yum -y install gcc gcc-c++ make openssl-devel pcre-devel         && mkdir -p /data         && cd /tmp/$NV         && ./configure --prefix=/data/nginx         && make -j 2         && make install         && echo "${TIME_ZOME}" > /etc/timezone         && ln -sf /usr/share/zoneinfo/${TIME_ZOME} /etc/localtime         && rm -rf /tmp/nginx*         && yum clean all         && yum -y remove gcc gcc-c++ makeWORKDIR /data/nginx/EXPOSE 80CMD ["./sbin/nginx","-g","daemon off;"]

(2) Prepare the Nginx configuration file.
Modify the server segment of the nginx.conf file as follows, I've removed the # comment line

    server {        listen       80;        server_name  localhost;        location / {            root   html;            index  index.html index.htm index.php;   #添加index.php        }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }        location ~ \.php$ {            root           html;            fastcgi_pass   php:9000;   #php容器名称和端口号            fastcgi_index  index.php;            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  #使用root指定的路径            include        fastcgi_params;        }    }

(3) Constructing nginx image

[[email protected] php]# cd ../nginx/[[email protected] nginx]# docker build -t nginx:1.15.5 .
Download MySQL image

The recommended direct use of the official MySQL image, I tested the source compiled installation, the last generated image size of more than 3G, and the official image only more than 400 m, and the source code installation operation is more complex, so here I abandoned the source installation, and choose to use the official image directly.

[[email protected] nginx]# docker pull mysql:8.0.128.0.12: Pulling from library/mysql802b00ed6f79: Pull complete 30f19a05b898: Pull complete 3e43303be5e9: Pull complete 94b281824ae2: Pull complete 51eb397095b1: Pull complete 54567da6fdf0: Pull complete bc57ddb85cce: Pull complete d6cd3c7302aa: Pull complete d8263dad8dbb: Pull complete 780f2f86056d: Pull complete 8e0761cb58cd: Pull complete 7588cfc269e5: Pull complete Digest: sha256:038f5f6ea8c8f63cfce1bce9c057ab3691cad867e18da8ad4ba6c90874d0537aStatus: Downloaded newer image for mysql:8.0.12

View images that have been built

[[email protected] nginx]# docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEnginx               1.15.5              2ecb7008e436        6 minutes ago       266MBphp                 7.2.11              5be80a8d0f1f        35 minutes ago      580MBcentos              latest              75835a67d134        2 days ago          200MBmysql               8.0.12              6a834f03bd02        5 weeks ago         484MB
Vi. creating and starting a container

(1) Creating a custom network Lnmp

[[email protected] nginx]# docker network create lnmpd56d132338f8e63537dac55d481053d56a5c5faf3ab3bd3a04ac2bc460b54b3f[[email protected] nginx]# docker network lsNETWORK ID          NAME                DRIVER              SCOPEb0fa374c72f1        bridge              bridge              locale6930ef7e774        host                host                locald56d132338f8        lnmp                bridge              locald7beee548faa        none                null                local

(2) Create scripts to generate MySQL, PHP, nginx containers
docker_lnmp.sh script content is as follows

#!/bin/bashfunction mysql(){    docker run --name mysql --restart=always --net lnmp -p 3306:3306     -v /data/mysql/data:/var/lib/mysql     -v /data/mysql/conf:/etc/mysql/conf.d     -v /data/mysql/logs:/logs     -e MYSQL_ROOT_PASSWORD=test123456     -d mysql:8.0.12 --character-set-server=utf8}function php(){    docker run --name php --restart=always --net lnmp     -v /data/nginx/html:/data/nginx/html     -v /data/php/log:/data/php/var/log     -d php:7.2.11}function nginx(){    docker run --name nginx --restart=always --net lnmp -p 80:80     -v /data/nginx/html:/data/nginx/html     -v /data/nginx/logs:/data/nginx/logs     -d nginx:1.15.5}$1

Note: PHP and Nginx must both mount the site Directory
(3) Start MySQL, PHP, Nginx container

[[email protected] ~]# sh docker_lnmp.sh mysql[[email protected] ~]# sh docker_lnmp.sh php[[email protected] ~]# sh docker_lnmp.sh nginx

(4) To see if the container has started successfully

[[email protected] ~]# docker psCONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                               NAMESa1f9c0dea6df        nginx:1.15.5        "./sbin/nginx -g ‘da…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp                  nginx8dfe39adbc60        php:7.2.11          "sbin/php-fpm -c etc…"   5 minutes ago        Up 5 minutes        9000/tcp                            php475b41187391        mysql:8.0.12        "docker-entrypoint.s…"   6 minutes ago        Up 6 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   mysql

(5) Modify MySQL password encryption method for Mysql_native_password
[[email protected] ~]# vim /data/mysql/conf/docker_mysql.cnf

[mysqld]default-authentication-plugin=mysql_native_password

If the encryption method is not modified, the lower version of the MySQL client will be error when landing
Restarting the MySQL container
[[email protected] ~]# docker restart mysql

Seven, testing

(1) Create MySQL test account

[[email protected] html]# docker exec -it mysql /bin/bash[email protected]:/# mysql -uroot -ptest123456mysql> create database test;mysql> create user [email protected] identified by ‘123456‘;mysql> grant all privileges on test.* to [email protected];mysql> exit[email protected]:/# exit

Note: There is a need to grant access to the PHP container, using PHP.LNMP
(2) write a PHP file to test the connection to MySQL
[[email protected] ~]# vim /data/nginx/html/test.php

<?php    echo "Hello PHP<br/>";    $conn = mysqli_connect("mysql","test","123456");    if(!$conn){        echo "连接数据库失败";    }else{        echo "连接数据库成功";    }    phpinfo();?>

Browser Access http://192.168.2.236/test.php

Eight, upload the created image to the private warehouse

(1) Building a private image warehouse
The construction of the private image warehouse I will not speak, can refer to my other post "Centos 7 building Docker Private Image Warehouse", http://blog.51cto.com/andyxu/2175081
(2) Make the MySQL container into a new image

[[email protected] ~]# docker commit -a "andyxu" -m "lnmp-mysql" mysql mysql:lnmp.8.0.12[[email protected] ~]# docker images


(3) Label Nginx, MySQL, and PHP images

[[email protected] ~]# docker tag mysql:lnmp.8.0.12 192.168.2.225:5000/mysql:lnmp.v1[[email protected] ~]# docker tag php:7.2.11 192.168.2.225:5000/php:lnmp.v1[[email protected] ~]# docker tag nginx:1.15.5 192.168.2.225:5000/nginx:lnmp.v1

(4) Upload the image to the private warehouse

[[email protected] ~]# docker push 192.168.2.225:5000/mysql:lnmp.v1[[email protected] ~]# docker push 192.168.2.225:5000/php:lnmp.v1[[email protected] ~]# docker push 192.168.2.225:5000/nginx:lnmp.v1

(5) View uploaded images.

[[email protected] ~]# curl http://192.168.2.225:5000/v2/_catalog{"repositories":["httpd-test","mysql","nginx","php"]}[[email protected] ~]# curl http://192.168.2.225:5000/v2/mysql/tags/list{"name":"mysql","tags":["lnmp.v1"]}[[email protected] ~]# curl http://192.168.2.225:5000/v2/php/tags/list{"name":"php","tags":["7.2-apache","lnmp.v1"]}[[email protected] ~]# curl http://192.168.2.225:5000/v2/nginx/tags/list{"name":"nginx","tags":["lnmp.v1"]}

(6) Package the image into a tar file and save it in the/tmp directory

[[email protected] ~]# docker save -o /tmp/mysql-lnmp.v1.tar 192.168.2.225:5000/mysql[[email protected] ~]# docker save -o /tmp/php-lnmp.v1.tar php:7.2.11[[email protected] ~]# docker save -o /tmp/nginx-lnmp.v1.tar nginx:1.15.5

Customizing LNMP environment Images with Dockerfile

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.