# 一:环境准备
centos7.4以及搭建好的Docker
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[[email protected] ~]# docker -v
Docker version 17.03.2-ce, build f5ec1e2
# 二:搭建nginx和php的镜像
1.nginx
创建基础镜像
[[email protected] /]# vim Dockerfile
FROM centosMAINTAINER liuning
CentOS-Base.repo.bak
RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak/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/WORKDIR /etc/yum.repos.d/
RUN yum install -y epel-release-latest-7.noarch.rpm
RUN yum clean allRUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel ntpdate crontabs
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
生成基础镜像
[[email protected] /]# docker build -t centos_init .
在根目录下创建nginx文件夹并进入
[[email protected] /]# mkdir nginx;cd nginx
编写nginx的Dockerfile
[[email protected] nginx]# vim Dockerfile
FROM centos_initMAINTAINER liuning
RUN useradd -M -s /sbin/nologin www
ADD nginx-1.8.1.tar.gz /usr/local/srcRUN 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 installCOPY 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 80CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
从github上获取需要的文件
https://github.com/jsonhc/docker_project/tree/master/docker_dockerfile/lnmp
最终目录下有这些文件
[[email protected] nginx]# ls
Dockerfile fastcgi_params nginx-1.8.1.tar.gz nginx.conf www.conf
构建nginx镜像
[[email protected] nginx]# docker build -t nignx .
查看是否构建成功
[[email protected] nginx]# docker images | grep nginx
nginx latest f453aeaced4a About an hour ago 583 MB
2.php
在根目录下创建php文件夹并进入
[[email protected] /]# mkdir php;cd php
编写Dockerfile文件
[[email protected] php]#vim Dockerfile
FROM centos_initMAINTAINER liuning
ADD libmcrypt-2.5.7.tar.gz /usr/local/src
WORKDIR /usr/local/src/libmcrypt-2.5.7
RUN ./configure && make && make installADD 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 installCOPY php.ini-production /usr/local/php/etc/php.ini
COPY php-fpm.conf.default /usr/local/php/etc/php-fpm.confRUN useradd -M -s /sbin/nologin php
RUN sed -i -e ‘[email protected];pid = 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.conf
RUN sed -i ‘[email protected];daemonize = [email protected] = [email protected]‘ /usr/local/php/etc/php-fpm.confEXPOSE 9000
CMD ["/usr/local/php/sbin/php-fpm"]
从github上获取需要的文件,如上最终php文件夹下有如下这些文件
[[email protected] php]# ls
Dockerfile init.d.php-fpm libmcrypt-2.5.7.tar.gz php-5.6.30.tar.bz2 php-fpm.conf.default php.ini-production
构建php镜像(过程漫长)
[[email protected] php]# docker build -t php .
查看是否构建成功
[[email protected] php]# docker images | grep php
php latest 15fe39a66344 35 minutes ago 1.12 GB
三:启动容器
启动php
[[email protected] php]# docker run -d --name=php -v /www:/usr/local/nginx/html php
启动nginx
docker run -d --name=nginx -p 80:80 -v /www:/usr/local/nginx/html --link=php:php nginx
查看启动是否正常,若是容器一启动就是exited状态,则使用docker logs -f 容器名,查看报错信息
[[email protected] php]# docker ps -a
在/www下创建phpinfo和html文件进行测试
[[email protected] www]# ls
index.html test.php
四:访问测试
原文地址:http://blog.51cto.com/11093860/2113499