首先我选择了在centos6里部署nginx的镜像,如果大家选择的是centos7,自己重新修改吧
这里的问题点有几个:
1,make的版本选择,因为我下载了最新的cmake,需要c++11编译
这玩意的安装比较麻烦,在前面一篇随笔里已经写过gcc在centos里的安装
2,docker容器的层级,我使用了 以下办法来减少层级,不在同一个层级,有些操作会出现问题哦
RUN .....
&& .....
3,CMD和Endpoint的使用,我这里只用了endpoint,但nginx作为后台运行,需要额外添加参数
ENTRYPOINT ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
4,调试,在调试过程中最好使用“docker run -itd nginx_centos6”,把-itd改为-it,这样能够查看到出错信息
5,真正部署时,我使用了dockercompose文件,可以让很多参数进行额外的配置
# Version 0.1 FROM centos:6 MAINTAINER kuba si812cn@163.com ENV NGINX_UPSYNC_MODULE_VERSION 2.1.0 ENV CMAKE_VERSION 3.12.4 ENV ZLIB_VERSION 1.2.11 ENV PCRE_VERSION 8.42 ENV OPENSSL_VERSION 1_1_1 ENV NGINX_HTTP_CONCAT_VERSION 1.2.2 ENV GEOIP_VERSION 1.4.8 ENV NGINX_VERSION 1.15.6 RUN mkdir -p /opt/software WORKDIR /opt/software/ RUN yum -y update;yum -y install epel-release wget tar xz unzip make autoconf automake curl curl-devel gcc gcc-c++ gcc-g77 kernel-devel gd gd-devel mlocate && yum -y install centos-release-scl-rh centos-release-scl && yum -y install devtoolset-6-gcc.x86_64 devtoolset-6-gcc-c++.x86_64 && yum -y install libtool && scl enable devtoolset-6 bash && updatedb && yum clean all && source /opt/rh/devtoolset-6/enable && export CC=/opt/rh/devtoolset-6/root/usr/bin/gcc && export CXX=/opt/rh/devtoolset-6/root/usr/bin/g++ && wget https://github.com/weibocom/nginx-upsync-module/archive/v${NGINX_UPSYNC_MODULE_VERSION}.zip && unzip v${NGINX_UPSYNC_MODULE_VERSION}.zip && rm -f v${NGINX_UPSYNC_MODULE_VERSION}.zip && wget https://github.com/Kitware/CMake/archive/v${CMAKE_VERSION}.zip && unzip v${CMAKE_VERSION}.zip && rm -f v${CMAKE_VERSION}.zip && cd CMake-${CMAKE_VERSION}/ && ./bootstrap && gmake && make install && cd ../ && wget https://github.com/madler/zlib/archive/v${ZLIB_VERSION}.zip && unzip v${ZLIB_VERSION}.zip && rm -f v${ZLIB_VERSION}.zip && cd zlib-${ZLIB_VERSION}/ && ./configure && make && make install && cd ../ && wget https://ftp.pcre.org/pub/pcre/pcre-${PCRE_VERSION}.zip && unzip pcre-${PCRE_VERSION}.zip && rm -f pcre-${PCRE_VERSION}.zip && cd pcre-${PCRE_VERSION}/ && ./configure --prefix=/usr/local/pcre && make && make install && cd ../ && wget https://github.com/openssl/openssl/archive/OpenSSL_${OPENSSL_VERSION}.zip && unzip OpenSSL_${OPENSSL_VERSION}.zip && rm -f OpenSSL_${OPENSSL_VERSION}.zip && cd openssl-OpenSSL_${OPENSSL_VERSION}/ && ./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl --shared && make && make install && cd /usr/local/ && ln -sf openssl ssl && echo "/usr/local/openssl/lib" >>/etc/ld.so.conf && ldconfig && echo "export OPENSSL=/usr/local/openssl/bin">>/etc/profile && echo "export PATH=$OPENSSL:$PATH">>/etc/profile && source /etc/profile && ln -sf /usr/local/openssl/bin/openssl /usr/bin/openssl && ln -sf /usr/local/openssl/include/openssl /usr/include/openssl && ln -sf /usr/local/openssl/lib/libcrypto.so.1.1 /lib/libcrypto.so.6 && ldconfig -v && cd /opt/software/ && wget https://github.com/alibaba/nginx-http-concat/archive/${NGINX_HTTP_CONCAT_VERSION}.zip && unzip ${NGINX_HTTP_CONCAT_VERSION}.zip && rm -f ${NGINX_HTTP_CONCAT_VERSION}.zip && wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz && tar zxvf GeoIP.tar.gz && rm -f GeoIP.tar.gz && cd GeoIP-${GEOIP_VERSION}/ && ./configure && make && make install && updatedb && locate libGeoIP.so.1 && echo "/usr/local/lib" >> /etc/ld.so.conf && ldconfig && cd ../ && wget https://github.com/nginx/nginx/archive/release-${NGINX_VERSION}.zip && unzip release-${NGINX_VERSION}.zip && rm -f release-${NGINX_VERSION}.zip && groupadd www && useradd -g www www -s /bin/false && cd nginx-release-${NGINX_VERSION}/ && ./auto/configure --prefix=/usr/local/nginx --user=www --group=www --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --without-http_memcached_module --with-http_ssl_module --with-openssl=/opt/software/openssl-OpenSSL_${OPENSSL_VERSION}/ --with-http_realip_module --with-http_image_filter_module --with-http_mp4_module --with-http_flv_module --with-pcre=/opt/software/pcre-${PCRE_VERSION}/ --with-zlib=/opt/software/zlib-${ZLIB_VERSION}/ --with-http_addition_module --with-http_geoip_module --with-http_sub_module --with-http_degradation_module --add-module=/opt/software/nginx-upsync-module-${NGINX_UPSYNC_MODULE_VERSION}/ --add-module=/opt/software/nginx-http-concat-${NGINX_HTTP_CONCAT_VERSION}/ && make && make install ADD ./nginx.conf /usr/local/nginx/conf/ ADD ./html/ /usr/local/nginx/html/ ENTRYPOINT ["/usr/local/nginx/sbin/nginx","-g","daemon off;"] EXPOSE 80 443
nginx.conf
user www www; worker_processes 4; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { worker_connections 8096; multi_accept on; use epoll; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; client_max_body_size 10M; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; error_page 404 /errors/404.html; error_page 500 502 503 504 /errors/50x.html; location ^~ /errors/ { root html; } location / { root html; index index.html index.htm; } } }
创建镜像
docker build -t nginx_centos6 ./
启动镜像
docker run -it nginx_centos6
这样启动后,正常来说使用http://127.0.0.1是访问拒绝的,因为Docker里有自己的IP,需要隐射一下
我选择使用了docker_compose来配置隐射
# Copyright IBM Corp. All Rights Reserved. # # SPDX-License-Identifier: Apache-2.0 # version: '2' services: nginx0: image: nginx_centos6 restart: always container_name: nginx0 ports: - 80:80
执行docker-compose -f docker-compose-nginx.yaml up -d 2>&1
一切OK