The original LNMP environment server is upgraded to a LNMP environment built mysql+nginx+php a single Docker container

Source: Internet
Author: User
Tags fpm mysql version php and mysql docker run

Date: February 2018

I. Project BACKGROUND

My unit is now the Web service architecture for the LNMP environment, server software, hardware upgrade deployment difficult, while open source software, considering the technology upgrade, the leader decided to upgrade the overall server architecture to the container architecture, maintainability, portability strong.

Two. Project description

The original server architecture for the LNMP environment, the server is stable, upgrade to a container architecture, should be smooth over, the use of a single image to construct a container, the container interconnect, deployed as a LNMP environment; The original server project code, configuration files, database data, such as the use of data mount method and the container interconnection, The access port uses port mapping to enable client access.

Three. Implementation steps

1. Mirroring construction

1) MySQL image construction

We use Dockerfile to construct vim Dockerfile

From mysql:5.7

Maintainer Yst <[email protected]>

ENV TZ "Asia/shanghai"

Base image is mysql5.7

Docker build-t MySQL.

Build to construct the image, MySQL for the mirrored warehouse and tag,. For the current path

The appearance of success on behalf of success.

2) PHP Image construction

Vim Dockerfile

From Centos:centos7 #以centos7为基础

Maintainer Yst <[email protected]> #制作者

ENV TZ "Asia/shanghai" #时区

#安装依赖包

RUN yum-y Install libxml2-devel libmcrypt-devel libmcrypt mcrypt mhash libjpeg libjpeg-devel libpng libpng-devel Net-too LS gcc gcc-c++ make

ADD php-5.6.36.tar.gz/#拷贝安装包

#运行源码包安装程序

RUN cd/php-5.6.36 &&/configure--prefix=/usr/local/php--with-mysql=mysqlnd--with-pdo-mysql=mysqlnd-- With-mysqli=mysqlnd--enable-mbstring--with-zlib--with-libxml-dir=/usr--enable-xml--enable-fpm--WITH-GD & & Make && make install

#复制文件
RUN Cp/php-5.6.36/php.ini-production/usr/local/php/etc/php.ini
RUN cp/usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf

#配置php

RUN useradd-m-s/sbin/nologin PHP

#修改配置文件
RUN sed-i-E ' s\;p id = run/php-fpm.pid\pid = Run/php-fpm.pid\g '-e ' s\nobody\php\g '-e ' S\listen = 127.0.0.1:9000\li Sten = 0.0.0.0:9000\g '/usr/local/php/etc/php-fpm.conf

RUN sed-i ' s\;d aemonize = yes\daemonize = No\g '/usr/local/php/etc/php-fpm.conf

EXPOSE 9000 #暴露端口
CMD ["/USR/LOCAL/PHP/SBIN/PHP-FPM"] #启动php-fpm

Docker build-t PHP.

3) Nginx Image Construction

Write the configuration file first nginx.conf

Vim nginx.conf

User Nginx; Worker_processes 1;

Error_log Logs/error.log; #error_log Logs/error.log Notice; #error_log Logs/error.log Info;

PID Logs/nginx.pid;

Events {worker_connections 1024;}

HTTP {

Include Mime.types;

Default_type Application/octet-stream;

Log_format Main ' $remote _addr-$remote _user [$time _local] "$request" '

' $status $body _bytes_sent ' $http _referer '

' "$http _user_agent" "$http _x_forwarded_for";

Access_log Logs/access.log Main;

Sendfile on; #tcp_nopush on;

Keepalive_timeout 65;

gzip on;

server {

Listen 80;

server_name localhost;

CharSet Utf-8;

Location/{

#存放网页代码目录

root HTML;

Index index.php index.html index.htm;

}

# REDIRECT Server error pages to the static page/50x.html

#重定向到错误页面

Error_page 502 503 504/50x.html;

Location =/50x.html {

root HTML;

}

# Proxy The PHP scripts to Apache listening on 127.0.0.1:80

#location ~ \.php$ {

# Proxy_pass http://127.0.0.1;

#}

#如果结尾为. php file, the container named PHP, is responsible for the container. PHP, and the port is 9000,

Location ~ \.php$ {

root HTML;

Fastcgi_pass php:9000;

Fastcgi_index index.php;

Fastcgi_param script_filename/$document _root$fastcgi_script_name;

Include Fastcgi_params;

}

}

}

From Centos:centos7
Maintainer yst [email protected]

RUN yum-y Install gcc gcc-c++ pcre-devel zlib-devel make
RUN Useradd nginx-s/sbin/nologin
ADD nginx-1.10.3.tar.gz/
RUN cd/nginx-1.10.3 &&./configure--prefix=/usr/local/nginx--user=nginx--group=nginx--with-http_stub_ Status_module--with-http_gzip_static_module--with-pcre && make && make install
#创建日志目录
RUN Mkdir/var/log/nginx
#拷贝配置文件
ADD nginx.conf/usr/local/nginx/conf/nginx.conf #将写好的配置文件添加到镜像中
EXPOSE 80 443
entrypoint ["/usr/local/nginx/sbin/nginx", "-G", "daemon off;"] #以守护进程方式运行nginx

Docker build-t Nginx.

2. Build and run the container

Because to upgrade to the LNMP of the Docker environment, the database is the background, so we should build the database container, build the PHP container, and finally generate the Nginx container.

1) Run and start the MySQL container

Docker run-d-it--name mysql-p 3306:3306-v/root/mysql/data:/var/lib/mysql-e mysql_root_password=123456 MySQL

-D: Run the container as a daemon

-I: Keep the container's input open

-T: Assigning a pseudo terminal

--name: The name of the container

-P: Map the port of the host to the port of the container

-E: Set the database login root password

-V Mount host/root/bo/data/mysql to the container's/var/lib/mysql, that is, the host and the container to achieve data synchronization

The last MySQL is the mirrored warehouse name and tag

2) Run and start the PHP container

Docker run-d-it--name php-p 9000:9000-v/root/php/data/:/usr/local/nginx/html--link mysql:mysql php

-V: Here for the directory of the host to mount the Nginx container in the Nginx installation directory of the Web page code storage directory, the. PHP end of the code file mount map to the directory

You can migrate the code from the old LNMP server to the host, enabling data to be connected to the container.

--link: For connection to MySQL container, name is MySQL

3) Run and start the Nginx container

Docker run-d-it--name nginx-p 80:80-v/root/nginx/data:/usr/local/nginx/html --link php:php ng Inx

--link: PHP for connection to PHP container

-V: The map host hosts the Web page code path to the container's Nginx installation directory of the page code directory, the original old LNMP server can be migrated to the host, the data and container interconnection

3. Testing

1) Prepare index.html,index.php,test.php

Normal Web files, PHP Web files, testing PHP and MySQL connectivity test files

Vim index.html

Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Vim index.php

<?php
Phpinfo ();
?>

The MySQL version here is 5.7, so the test code is mysqli_connect (' the container name in the host: corresponding host port ', ' Login server username ', ' password ')

If it is another version, the statement will be different

Mysqli_close ($link); To close the connection

Vim test.php

<?php

$link =mysqli_connect (' mysql:3306 ', ' root ', ' 123456 ');

if ($link) echo "Connection succeeded";

Mysqli_close ($link);

?>

2) Access test

Open Browser, enter HTTP/host IP return

Show us the index.html we prepared.

Enter HTTP//host's ip/index.php carriage return

Enter HTTP//host's ip/test.php carriage return

3) database Operation test

Open Navicat for MySQL

Create a new connection

Host name is the IP of the host, why this is the IP of the host, because the database container is established on the host, and the host and the container when the container is running with the parameter-p 3,306:3,306 port mapping, so access to the host 3306 is equivalent to access the container 3306 port, This is the MySQL service that accesses the MySQL container.

User name is root

The password is the ROOT password of the-e mysql_root_password=123456 set when we run the container 123456

By establishing a connection, we can operate the container on the Windows desktop, and the results of the operation will be fed back into the container for data interconnection.

4. Summary

Project Difficulty

1) How to generate the appropriate image

MySQL image is we download the MySQL pure image in the Docker Image Library, the Docker tag command marks the original mirror tag as our own name

PHP image is based on centos7 pure image, through the source installation, configuration configuration file, and open the port built

Nginx image is based on centos7 pure image, through the source installation, modify the configuration file, open the port and build

2) How to properly boot the image and run the container

Start the MySQL container first, when you open the PHP container and the Nginx container,

After the container runs the command executes, the container is closed, which is not the result we want, we want the container to run the service, we can access the service inside the container.

We start with the query image generation file Dockerfile file, step by step to find the cause, after repeated testing, constantly generate images, run the container, I found that at the end of the image generation, we must open the service in this image, such as PHP image, when we run the container, The PHP-FPM service needs to be opened in the container, which requires us to start the service process when creating the image, which is

CMD ["/USR/LOCAL/PHP/SBIN/PHP-FPM"], while Nginx container runs with the same problem, repeated query, debugging, should be in the image to execute the import command

entrypoint ["/usr/local/nginx/sbin/nginx", "-G", "daemon off;"), or the command to run the container to join the--restart=always parameter otherwise after the container starts running the portal name, The container will also stop.

3) How to connect each container

A single container, each of which provides each service function, is a container for a Docker container, a feature that is the advantage of a Docker container, each container provides a service that encapsulates it, runs it when it is not used, can be migrated when it is not available, and can provide services wherever it is copied. How we implement it is to use the--ling parameter to interconnect several associated containers together to complete a more powerful service function.

For example: to build a container environment lnmp, need to mysql,php,nginx several containers to provide services, need to connect several containers together to complete the LNMP service

Docker run-d-it--name nginx-p 80:80-v/root/nginx/data:/usr/local/nginx/html--link php:php--link mysql:mysql Nginx

The original LNMP environment server is upgraded to a LNMP environment built mysql+nginx+php a single Docker container

Related Article

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.