您的位置:首页 > 运维架构 > Docker

docker:构建nginx+php-fpm镜像(一):构建nginx自启动镜像

2017-03-09 14:02 591 查看

步骤一:手动安装nginx环境,并记录全过程:

#使用yum更新系统
yum -y update

#下面编译安装tengine,查看有哪些包需要安装
#安装wget包,用于获取安装软件包
yum -y install wget

cd /usr/local/src
#下载nginx安装包,并指定放置目录/usr/local/src
wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz -P /usr/local/src/
#z匹配.gz x解压 f本地
tar -zxf tengine-2.1.2.tar.gz

#创建nginx需要的用户和组
groupadd -r nginx
useradd -g nginx -M nginx

#tengine初次编译
./configure --prefix=/usr/local/tengine \
--user=nginx --group=nginx

#报错以及补充系统包:
yum -y install gcc pcre-devel openssl-devel

#初次编译通过,增加nginx模块配置再次编译:
--with-http_realip_module #nginx获取客户端真实IP的模块
--with-http_sub_module #用于替换网站页面关键字--平常基本没啥用
--with-http_gzip_static_module #静态缓存压缩,用于减少流量
--with-http_gunzip_module #为不支持gzip模块的客户端提供解压缩服务,用于节省空间减少IO
--with-http_ssl_module #使nginx支持https
--with-pcre #nginx支持pcre正则,主要是支持rewrite功能
--with-http_stub_status_module #用于监控nginx状态

完整编译命令如下:

./configure --prefix=/usr/local/tengine \
--user=nginx --group=nginx \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_gunzip_module \
--with-pcre \
--with-http_stub_status_module \
--with-http_ssl_module


#编译安装
make & make install

#启动nginx
/usr/local/tengine/sbin/nginx

步骤二:根据以上步骤构建dockerfile生成nginx镜像

Dockerfile

###################################################
#Dockerfile to build mysqlDB container images
#base on centos
###################################################
#Set the base image to Centos6
FROM centos:6
#File Suthor
MAINTAINER liujian@wanbu.com.cn

#install basic rpm
RUN yum -y update
RUN yum -y install wget gcc pcre-devel openssl-devel
RUN wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz -P /usr/local/src/ \
&& groupadd -r nginx && useradd -g nginx -M nginx
RUN cd /usr/local/src/ \
&& tar -zxf tengine-2.1.2.tar.gz \
&& cd tengine-2.1.2/ \
&& ./configure --prefix=/usr/local/tengine --user=nginx --group=nginx --with-http_realip_module --with-http_gzip_static_module --with-http_gunzip_module --with-pcre --with-http_stub_status_module --with-http_ssl_module \
&& make && make install \
&& ln -s /usr/local/tengine/sbin/nginx /usr/sbin/nginx \
&& yum -y remove wget gcc pcre-devel openssl-devel
ADD nginx.conf /usr/local/tengine/conf/
EXPOSE 80
CMD ["nginx","-g","deamon off;"]


#制定Dokcerfile构建nginx镜像
docker build -t nginx_test:v1.0 .

#使用镜像创建容器,测试nginx运行情况
docker run -itd -p 0.0.0.0:7788:80 --name nginx_image_test nginx_test:v1.0

#访问正常,但是nginx配置文件无法修改,只能启动运行。
如果在测试环境需要修改配置文件重启等,可以使用:
#将dockerfile内部CMD ["nginx","-g","deamon off;"]替换为
ENTRYPOINT /usr/sbin/nginx -c /etc/nginx/nginx.conf && /bin/bash
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: