nginx怎么在docker容器中自动生成配置文件

实现思路

最后运行的命令大概是这样:

docker run -d -p 80:80 -e xxx=xx 镜像名称 镜像中脚本路径

这里的脚本会代替dockerfile中的cmd指令,所以我们要构建一个自动生成且启动nginx的shell脚本。

#!/bin/bash  #从环境变量里面获取lt开头,为了与其他环境变量区别开,例如lt_analysis=172.17.0.1:8083 result="" for a in $(env | grep ^lt) do  old_ifs="$ifs"  ifs="_"  arr=($a)  b=${arr[1]}  ifs="="  arr=($b)  ifs="$old_ifs"  result="${result}   location /${arr[0]}/ {     proxy_pass  http://${arr[1]}/${arr[0]}/;     proxy_connect_timeout 90;     proxy_send_timeout 90;     proxy_read_timeout 90;   }" done #将nginx配置文件中nginx_conf中置换成变量result sed -i "s|nginx_conf|$(echo ${result})|g" /etc/nginx/nginx.conf cd /usr/sbin ./nginx

需要说明的一点是业务中并不需要将整个配置文件生成,只需要将其中location生成然后替换原配置文件中标记的位置,下面就是原配置文件标记的位置。

http {   ...      server {     ...      location / {       root  html;       index index.html index.htm;     }      nginx_conf      #error_page 404       /404.html;     ...

我以为将这个shell脚本和默认的配置文件放入nginx的dockerfile镜像中,然后就成功了,但是…运行上述命令之后容器没有起来,查看容器日志,出来的信息却是***syntax error: “(” unexpected***。我的shell脚本在centos上经过测试是可以运行的,那么为什么会报这个错呢? 经过排查,原来是dockerfile使用基础镜像是官方nginx,官方的镜像使用ubuntu不再使用bash来而是dash执行shell脚本,真是个坑 。没办法我只好修改dockerfile,下面就是使用基础镜像centos。

from centos  env nginx_version 1.10.3 env openssl_version 1.0.2k env pcre_version 8.40 env zlib_version 1.2.11 env build_root /usr/local/xx/nginx  # 为了减小最终生成的镜像占用的空间,这里没有执行yum update命令,可能不是好的实践 # 为了加快构建速度,这里使用了163的安装源 #run yum -y update  run yum -y install curl    && mv /etc/yum.repos.d/centos-base.repo /etc/yum.repos.d/centos-base.repo.backup    && curl http://mirrors.163.com/.help/centos7-base-163.repo -o /etc/yum.repos.d/centos7-base-163.repo     && yum -y install gcc gcc-c++ make perl zip unzip    && mkdir -p $build_root    && cd $build_root    && curl https://ftp.pcre.org/pub/pcre/pcre-$pcre_version.zip -o $build_root/pcre-$pcre_version.zip    && curl https://www.openssl.org/source/openssl-$openssl_version.tar.gz -o $build_root/openssl-$openssl_version.tar.gz    && curl http://www.zlib.net/zlib-$zlib_version.tar.gz -o $build_root/zlib-$zlib_version.tar.gz    && curl https://nginx.org/download/nginx-$nginx_version.tar.gz -o $build_root/nginx-$nginx_version.tar.gz    && tar vxzf nginx-$nginx_version.tar.gz    && unzip pcre-$pcre_version.zip    && tar vxfz zlib-$zlib_version.tar.gz    && tar vxfz openssl-$openssl_version.tar.gz    && cd nginx-$nginx_version    && build_config="     --prefix=/etc/nginx      --sbin-path=/usr/sbin/nginx      --conf-path=/etc/nginx/nginx.conf      --error-log-path=/var/log/nginx/error.log      --http-log-path=/var/log/nginx/access.log      --pid-path=/var/run/nginx.pid      --lock-path=/var/run/nginx.lock      --http-client-body-temp-path=/var/cache/nginx/client_temp      --http-proxy-temp-path=/var/cache/nginx/proxy_temp      --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp      --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp      --http-scgi-temp-path=/var/cache/nginx/scgi_temp      --with-openssl=$build_root/openssl-$openssl_version      --with-pcre=$build_root/pcre-$pcre_version      --with-zlib=$build_root/zlib-$zlib_version      --with-http_ssl_module      --with-http_v2_module       --with-threads      "    && mkdir -p /var/cache/nginx    && ./configure $build_config    && make && make install    && rm -rf $build_root    && yum -y remove gcc gcc-c++ make perl zip unzip    && yum clean all  #替换nginx默认文件 copy nginx.conf /etc/nginx/ #添加自动生成配置文件的shell脚本 copy 脚本名称 /root/  #暴露端口 expose 80 443  cmd ["nginx", "-g", "daemon off;"]

提醒:docker容器不支持后台运行,当命令执行之后,容器也会自然退出,这里我们需要将nginx配置文件设置一下

#user nobody; worker_processes 1;  #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;  #pid    logs/nginx.pid; daemon off;  //这里添加,关闭后台运行 events {   worker_connections 1024; }   http {

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享