准备

1.基础镜像centos的目录结构

[root@izwz91vd dockerfile]# tree centos
centos
├── CentOS7-Base-163.repo
├── Dockerfile
└── epel-release-latest-7.noarch.rpm

0 directories, 3 files

2.nginx目录结构:

[root@izwz91vd dockerfile]# tree nginx
nginx
├── Dockerfile
├── fastcgi_params
├── nginx-1.8.1.tar.gz
├── nginx.conf
└── www.conf

0 directories, 5 files

3.php的目录结构

[root@izwz91vd dockerfile]# 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

0 directories, 6 files

Dockerfile

1.centos

# base image
FROM centos

# MAINTAINER
MAINTAINER temerx@foxmail.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

运行:docker build -t centos_base .

2.nginx

FROM centos_base

MAINTAINER temerx@foxmail.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;"]

运行:docker build -t nginx .

3.php

FROM centos_base

MAINTAINER temerx@foxmail.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"]

运行:docker build -t php .

运行

利用构建的镜像启动php、nginx服务:

docker run -d --name=php -v /www:/usr/local/nginx/html php
docker run -d --name=nginx -p80:80 -v /www:/usr/local/nginx/html --link=php:php nginx