[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