1、创建项目目录并上传包
$ mkdir docker_nginx $ cd docker_nginx
下载nginx包
$ wget http://nginx.org/download/nginx-1.8.0.tar.gz
2、编辑Dockerfile
# From表示使用centos:latest这个镜像为基础构建我们的镜像 FROM centos:latest # 创建者的基本信息 MAINTAINER xiaozhou ([email protected]) LABEL Discription="基于centos的nginx镜像" version="1.0" # put nginx-1.8.0.tar.gz into /usr/local/src and unpack nginx ADD nginx-1.8.0.tar.gz /usr/local/src #安装nginx所依赖的包 RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel \ && yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel \ && useradd -M -s /sbin/nologin nginx # change dir to /usr/local/src/nginx-1.8.0 WORKDIR /usr/local/src/nginx-1.8.0 # execute command to compile nginx RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx \ --with-file-aio --with-http_ssl_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 # nginx_config RUN mkdir /usr/local/nginx/logs && mkdir /usr/local/nginx/run \ && sed -e '3i\user nginx;' -i /usr/local/nginx/conf/nginx.conf \ && sed -e "9i\error_log /usr/local/nginx/logs/error.log;" -i /usr/local/nginx/conf/nginx.conf \ && sed -e "12i\pid /usr/local/nginx/run/nginx.pid;" -i /usr/local/nginx/conf/nginx.conf \ && sed -e "29i\ \taccess_log /usr/local/nginx/logs/access.log;" -i /usr/local/nginx/conf/nginx.conf # logrotate RUN touch /etc/logrotate.d/nginx \ && echo -e "$LOGS_DIR/*.log {" >> /etc/logrotate.d/nginx \ && echo -e "\tdaily" >> /etc/logrotate.d/nginx \ && echo -e "\trotate" >> /etc/logrotate.d/nginx \ && echo -e "\tmissingok" >> /etc/logrotate.d/nginx \ && echo -e "\tdateext" >> /etc/logrotate.d/nginx \ && echo -e "\tcompress" >> /etc/logrotate.d/nginx \ && echo -e "\tdelaycompress" >> /etc/logrotate.d/nginx \ && echo -e "\tnotifempty" >> /etc/logrotate.d/nginx \ && echo -e "\tsharedscripts" >> /etc/logrotate.d/nginx \ && echo -e "\tpostrotate" >> /etc/logrotate.d/nginx \ && echo -e "\t/usr/bin/kill -USR1 \`cat $LOGS_DIR/nginx.pid\`" >> /etc/logrotate.d/nginx \ && echo -e "\tendscript" >> /etc/logrotate.d/nginx \ && echo -e "\t}" >> /etc/logrotate.d/nginx ENV PATH /usr/local/nginx/sbin:$PATH EXPOSE 80 CMD /bin/sh -c 'nginx -g "daemon off;"'
3、构建镜像
$ docker build -t centos:nginx .
4、启动容器
$ docker run -it -d -p 80:80 centos:nginx
浏览器输入宿主机地址:
192.168.121.121
可以看到 Welcome to nginx! 的页面,说明环境构建没问题。