小知识:nginx在docker容器中自动生成配置文件

公司在搭建docker自动化部署时,需要制作一个nginx镜像在其docker run时通过外部指定环境变量使得容器中的配置文件自动生成,不需要再到容器里改配置文件

实现思路

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/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生成然后替换原配置文件中标记的位置,下面就是原配置文件标记的位置。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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配置文件设置一下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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 {

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://juejin.im/post/5cf2a76be51d45590a445aeb

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:linux如何无损调整分区大小

2023-4-2 23:56:32

建站知识

小知识:详解nginx前端根据$remote_addr分发方法

2023-4-3 0:55:38

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索