檢視nginx和php的目錄結構:
[root@docker docker_demo]# tree nginx nginx ├── Dockerfile ├── fastcgi_params ├── nginx-1.8.1.tar.gz ├── nginx.conf └── www.conf
[root@docker docker_demo]# tree php php ├── Dockerfile ├── init.d.php-fpm ├── libmcrypt-2.5.7.tar.gz ├── php-5.6.30.tar.bz2 ├── php-fpm.conf.default └── php.ini-production
這裡將詳細講述nginx和php的構建過程,以及構建過程中用到的所有工具包和配置檔案
首先介紹nginx的構建,檢視nginx的Dockerfile:
[root@docker nginx]# cat Dockerfile FROM centos_init:v2 MAINTAINER json_hc@163.com RUN useradd -M -s /sbin/nologin www ADD nginx-1.8.1.tar.gz /usr/local/src RUN yum install libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel WORKDIR /usr/local/src/nginx-1.8.1 RUN ./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 install COPY nginx.conf /usr/local/nginx/conf/nginx.conf COPY fastcgi_params /usr/local/nginx/conf/fastcgi_params RUN mkdir /usr/local/nginx/conf/vhost COPY www.conf /usr/local/nginx/conf/vhost/www.conf EXPOSE 80 CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
從上面的基礎映象可以看見是centos_init:v2,這裡貼出該映象的Dockerfile:
[root@docker nginx]# cat ../init/Dockerfile # base image FROM centos # MAINTAINER MAINTAINER json_hc@163.com # backup CentOS-Base.repo to CentOS-Base.repo.bak RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak # add epel and aliyun repo to /etc/yum.repos.d COPY CentOS7-Base-163.repo /etc/yum.repos.d/CentOS7-Base-163.repo COPY epel-release-latest-7.noarch.rpm /etc/yum.repos.d/ # install epel.repo WORKDIR /etc/yum.repos.d/ RUN yum install -y epel-release-latest-7.noarch.rpm RUN yum clean all # running required command RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel ntpdate crontabs # change timzone to Asia/Shanghai RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
centos_init:v2映象新增了repo的環境和編譯的環境,而centos映象就是初始的官方映象
下面迴歸到nginx的構建檔案:
從nginx的Dockerfile檔案中可以看出,安裝nginx採用的編譯安裝,建立了使用者www和安裝了nginx的一些依賴包,copy了一些配置檔案到映象中,這裡
用到的配置檔案將會全部放置到github上供參考,然後通過Dockerfile進行構建nginx映象:
# docker build -t nginx:v1 .
這裡需要介紹配置檔案:
[root@docker nginx]# cat www.conf server { listen 80; root /usr/local/nginx/html; index index.htm index.html index.php; location ~ \.php$ { root /usr/local/nginx/html; fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
可以看見fastcgi_pass php:9000;
這是因為php也是一個容器,和nginx是隔離的,後面nginx將會通過--link的方式與php映象進行互聯訪問
檢視php的Dockerfile檔案:
[root@docker php]# cat Dockerfile FROM centos_init:v2 MAINTAINER json_hc@163.com ADD libmcrypt-2.5.7.tar.gz /usr/local/src WORKDIR /usr/local/src/libmcrypt-2.5.7 RUN ./configure && make && make install ADD php-5.6.30.tar.bz2 /usr/local/src RUN yum -y install libxml2 libxml2-devel bzip2 bzip2-devel libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel libcurl libcurl-devel WORKDIR /usr/local/src/php-5.6.30 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 install COPY php.ini-production /usr/local/php/etc/php.ini COPY php-fpm.conf.default /usr/local/php/etc/php-fpm.conf RUN useradd -M -s /sbin/nologin php RUN sed -i -e 's@;pid = run/php-fpm.pid@pid = run/php-fpm.pid@g' -e 's@nobody@php@g' -e 's@listen = 127.0.0.1:9000@listen = 0.0.0.0:9000@g' /usr/local/php/etc/php-fpm.conf RUN sed -i 's@;daemonize = yes@daemonize = no@g' /usr/local/php/etc/php-fpm.conf EXPOSE 9000 CMD ["/usr/local/php/sbin/php-fpm"]
構建的服務必須執行在前臺,而對於nginx來說:
daemon off表示將後臺執行關閉了,於是執行在前臺
而對於phh:sed -i 's@;daemonize = yes@daemonize = no@g' /usr/local/php/etc/php-fpm.conf
這裡也是將daemon模式關閉了,於是/usr/local/php/sbin/php-fpm執行在前臺
開始進行構建php:
[root@docker php]# docker build -t php .
檢視生成的映象:
[root@docker php]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE php latest 8902ce599658 5 minutes ago 1.08GB nginx latest c3babfeba09b 22 minutes ago 578MB
利用構建的映象啟動php、nginx服務:
[root@docker php]# docker run -d --name=php -v /www:/usr/local/nginx/html php 538d9866defefe8c818fbebc7109a1cf8d271583f7ce6d14d4483a103a212903 [root@docker php]# docker run -d --name=nginx -p80:80 -v /www:/usr/local/nginx/html --link=php:php nginx c476e0e2b37f5400ea2175b9a3fc61636190727576187f3feb9248fea37ffd81
上面啟動php的容器時,使用了-v進行對映,如果這裡不進行對映,那麼php的程式會啟動,但是遇到php結尾的檔案將不會解析,出現file not found的錯誤
檢視容器狀態:
[root@docker php]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c476e0e2b37f nginx "/usr/local/nginx/..." 11 seconds ago Up 10 seconds 0.0.0.0:80->80/tcp nginx 538d9866defe php "/usr/local/php/sb..." 39 seconds ago Up 38 seconds 9000/tcp php
網站目錄結構:
[root@docker www]# tree .
.
├── index.html
└── test.php
進入到php容器檢視hosts檔案:
[root@docker php]# docker exec -it php /bin/bash [root@538d9866defe php-5.6.30]# cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.4 538d9866defe
檢視nginx的hosts檔案:
[root@docker php]# docker exec -it nginx /bin/bash [root@c476e0e2b37f nginx-1.8.1]# cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.4 php 538d9866defe 172.17.0.5 c476e0e2b37f
可以看見有一條php的解析,這就是為什麼nginx能夠和php進行通訊的緣由(通過--link進行指定)
配置檔案託管到github:https://github.com/jsonhc/docker_project/tree/master/docker_dockerfile/lnmp