View the directory structure of Nginx and PHP:
[email protected] docker_demo]# tree Nginxnginx├──dockerfile├──fastcgi_params├──nginx-1.8. 1. Tar . gz├──nginx.conf└──www.conf
[email protected] docker_demo]# tree phpphp├──dockerfile├──init.d.php-fpm├──libmcrypt-2.5 . 7. Tar . gz├──php-5.6. . Tar . bz2├──php-fpm.conf.default└──php.ini-production
The build process for Nginx and PHP is described in detail, as well as all the toolkits and configuration files used in the build process.
First, we introduce the construction of Nginx and see the Dockerfile of Nginx:
[Email protected] nginx]#CatDockerfile from Centos_init:v2maintainer [email protected]163. Comrun Useradd-m-s/sbin/nologin wwwadd Nginx-1.8.1.Tar. gz/usr/local/SrcrunYum InstallLibxslt-devel-y gd gd-devel GeoIP geoip-devel pcre pcre-Develworkdir/usr/local/src/nginx-1.8.1RUN./configure--user=www--group=www--prefix=/usr/local/nginx--with-file-aio--with-ipv6--with-http_ssl_module--with-http_spdy_module--with-http_realip_module--with-http_addition_ Module--with-http_xslt_module--with-http_image_filter_module--with-http_geoip_module--with-http_sub_module- -with-http_dav_module--with-http_flv_module--with-http_mp4_module--with-http_gunzip_module--with-http_gzip_ Static_module--with-http_auth_request_module--with-http_random_index_module--with-http_secure_link_module-- With-http_degradation_module--with-http_stub_status_module && Make&& Make InstallCOPY nginx.conf/usr/local/nginx/conf/nginx.confcopy Fastcgi_params/usr/local/nginx/conf/Fastcgi_paramsrunmkdir/usr/local/nginx/conf/vhostcopy www.conf/usr/local/nginx/conf/vhost/Www.confEXPOSE theCMD ["/usr/local/nginx/sbin/nginx","- G","daemon off;"]
From the underlying image above you can see that it is centos_init:v2, where the dockerfile of the image is posted:
[Email protected] nginx]#Cat.. /init/dockerfile# Base Imagefrom centos# maintainermaintainer [email protected]163. com# Backup CentOS-base.repo to centos-Base.repo.bakRUNMV/etc/Yum. repos.d/centos-base.repo/etc/Yum. repos.d/centos-base.repo.bak# Add Epel and Aliyun repo to/etc/Yum. Repos.dcopy CentOS7-base-163. repo/etc/Yum. repos.d/centos7-base-163. Repocopy Epel-release-latest-7. noarch.rpm/etc/Yum. repos.d/# InstallEpel.repoworkdir/etc/Yum. repos.d/RUNYum Install-Y epel-release-latest-7. noarch.rpm RUNYumClean all # running required CommandrunYum Install-YGCC GCC-c++ glibc Makeautoconf OpenSSL openssl-devel ntpdate crontabs# change Timzone to Asia/ShanghairunCP/usr/share/zoneinfo/asia/shanghai/etc/localtime
The CENTOS_INIT:V2 image adds repo environment and a compiled environment, and the CentOS image is the original official image
The following regression to the Nginx build file:
From the Nginx dockerfile file can be seen, install Nginx installed by the installation, created a user www and installed nginx Some of the dependencies, copy some configuration files into the image, here
The configuration files used will all be placed on GitHub for reference, and then the Nginx image can be constructed via Dockerfile:
# docker Build-t Nginx:v1.
Here you need to describe the configuration file:
Cat www.conf Server { listen ; /usr/local/nginx/html; Index index.htm index.html index.php; ~ \.php$ { /usr/local/nginx/html; Fastcgi_pass php:9000; Fastcgi_index index.php; Fastcgi_param script_filename $document _root$fastcgi_script_name; Include Fastcgi_params; } }
can see Fastcgi_pass php:9000;
This is because PHP is also a container, and nginx is isolated, after the nginx will be--link in the way with the PHP image for internet access
View php dockerfile files:
[Email protected] php]#CatDockerfile from Centos_init:v2maintainer [email protected]163. Comadd Libmcrypt-2.5.7.Tar. gz/usr/local/Srcworkdir/usr/local/src/libmcrypt-2.5.7RUN./configure && Make&& Make InstallADD PHP-5.6. -.Tar. bz2/usr/local/SrcrunYum-YInstallLIBXML2 Libxml2-develbzip2 bzip2-devel Libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel libcurl libcurl-Develworkdir/usr/local/src/php-5.6. -RUN./configure--prefix=/usr/local/php--with-pdo-mysql=mysqlnd--with-mysqli=mysqlnd--with-mysql=mysqlnd-- With-openssl--enable-mbstring--with-freetype-dir--with-jpeg-dir--with-png-dir--with-mcrypt--with-zlib--with-libxml-dir=/USR--enable-xml--enable-sockets--enable-fpm--with-config-file-path=/usr/local/php/etc--with-bz2--WITH-GD && Make&& Make InstallCOPY php.ini-production/usr/local/php/etc/php.inicopy PHP-fpm.conf.default/usr/local/php/etc/php-Fpm.confrun Useradd-m-s/sbin/Nologin Phprunsed-i-e'[email protected];p id = run/[email protected] = Run/[email protected]'-E'[email protected]@[email protected]'-E'[email protected] = 127.0.0.1:[email protected] = 0.0.0.0:[email protected]'/usr/local/php/etc/php-Fpm.confrunsed-I.'[email protected];d aemonize = [email protected] = [email protected]'/usr/local/php/etc/php-Fpm.confexpose9000CMD ["/USR/LOCAL/PHP/SBIN/PHP-FPM"]
The built service must run in the foreground, and for Nginx:
Daemon off means the background run is off, so it runs in the foreground
and for phh:sed-i ' [email protected];d aemonize = [email protected] = [email protected] '/usr/local/php/etc/php-fpm.conf
This is also the daemon mode is turned off, so/usr/local/php/sbin/php-fpm run in the foreground
To start building PHP:
[[email protected] php]# Docker build-t PHP.
To view the generated image:
[email protected] php]# Docker images REPOSITORY TAG IMAGE ID CREATED sizephp latest 8902ce599658 5 minutes ago 1 . 08GBnginx Latest c3babfeba09b minutes ago 578MB
Start PHP and Nginx services with a built-in image:
[email protected] php]# Docker run-d--name=php-v/www:/usr/local/nginx/-D--name=nginx-p80: -v/w ww:/usr/local/nginx/html--link=php:php Nginxc476e0e2b37f5400ea2175b9a3fc61636190727576187f3feb9248fea37ffd81
When you start the PHP container, using the-v mapping, if not mapped, then the PHP program will start, but the end of the PHP files will not be resolved, the file not found error
To view the container status:
[email protected] php]# DockerPS-acontainer ID IMAGE COMMAND CREATED STATUS PORTS namesc476e0e2b37f Nginx"/usr/local/nginx/ ..." OneSeconds ago UpTenSeconds0.0.0.0: the- the/TCP Nginx538d9866defe PHP"/USR/LOCAL/PHP/SB ..." theSeconds ago Up -Seconds9000/tcp PHP
Site Directory structure:
[Email protected] www]# tree. ├──index.html└──test.php
Go to the PHP container to view the Hosts file:
[email protected] php]# Docker exec-it php/bin/bash[[email protected] PHP-5.6. -]#Cat/etc/hosts127.0.0.1localhost::1localhost ip6-localhost ip6-loopbackfe00::0ip6-localnetff00::0ip6-mcastprefixff02::1ip6-allnodesff02::2ip6-allrouters172.17.0.4538d9866defe
View Nginx's Hosts file:
[email protected] php]# Docker exec-it nginx/bin/Bash[[email protected] Nginx-1.8.1]#Cat/etc/hosts127.0.0.1localhost::1localhost ip6-localhost ip6-loopbackfe00::0ip6-localnetff00::0ip6-mcastprefixff02::1ip6-allnodesff02::2ip6-allrouters172.17.0.4PHP 538d9866defe172.17.0.5c476e0e2b37f
You can see that there is a PHP parsing, which is why Nginx can communicate with PHP for the reason (specified by--link)
Configuration file Hosting to Github:https://github.com/jsonhc/docker_project/tree/master/docker_dockerfile/lnmp
Dockerfile build nginx and combine PHP