nginx在docker容器中自動生成配置文件
公司在搭建docker自動化部署時,需要制作一個nginx鏡像在其docker run時通過外部指定環(huán)境變量使得容器中的配置文件自動生成,不需要再到容器里改配置文件。
實現(xiàn)思路
最后運行的命令大概是這樣:
docker run -d -p 80:80 -e xxx=xx 鏡像名稱 鏡像中腳本路徑
這里的腳本會代替dockerfile中的CMD指令,所以我們要構(gòu)建一個自動生成且啟動nginx的shell腳本。
#!/bin/bash
#從環(huán)境變量里面獲取lt開頭,為了與其他環(huán)境變量區(qū)別開,例如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
需要說明的一點是業(yè)務(wù)中并不需要將整個配置文件生成,只需要將其中l(wèi)ocation生成然后替換原配置文件中標(biāo)記的位置,下面就是原配置文件標(biāo)記的位置。
http {
...
server {
...
location / {
root html;
index index.html index.htm;
}
nginx_conf
#error_page 404 /404.html;
...
我以為將這個shell腳本和默認(rèn)的配置文件放入nginx的dockerfile鏡像中,然后就成功了,但是...運行上述命令之后容器沒有起來,查看容器日志,出來的信息卻是***Syntax error: “(” unexpected***。我的shell腳本在centos上經(jīng)過測試是可以運行的,那么為什么會報這個錯呢? 經(jīng)過排查,原來是dockerfile使用基礎(chǔ)鏡像是官方nginx,官方的鏡像使用Ubuntu不再使用bash來而是dash執(zhí)行shell腳本,真是個坑 。沒辦法我只好修改dockerfile,下面就是使用基礎(chǔ)鏡像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
# 為了減小最終生成的鏡像占用的空間,這里沒有執(zhí)行yum update命令,可能不是好的實踐
# 為了加快構(gòu)建速度,這里使用了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默認(rèn)文件
COPY nginx.conf /etc/nginx/
#添加自動生成配置文件的shell腳本
COPY 腳本名稱 /root/
#暴露端口
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]
提醒:docker容器不支持后臺運行,當(dāng)命令執(zhí)行之后,容器也會自然退出,這里我們需要將nginx配置文件設(shè)置一下
#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; //這里添加,關(guān)閉后臺運行
events {
worker_connections 1024;
}
http {
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
docker使用Dockerfile構(gòu)建鏡像的實現(xiàn)示例
本文主要介紹了docker使用Dockerfile構(gòu)建鏡像的實現(xiàn)示例,通過編寫 Dockerfile,您可以定義鏡像的基礎(chǔ)環(huán)境、安裝軟件包、復(fù)制文件、設(shè)置環(huán)境變量等操作,下面就來介紹一下2024-01-01
使用Docker compose啟動自定義jar包的步驟詳解
這篇文章主要介紹了使用Docker compose啟動自定義jar包的步驟,首先我們需要編寫一個docker-compose.yml文件來定義我們的服務(wù)傳到我們的云服務(wù)器上,本文給大家分享示例代碼,感興趣的朋友一起看看吧2024-03-03
Windows?Server?2016中文版安裝docker的詳細(xì)步驟
因業(yè)務(wù)需要所以需要安裝Docker,但是在途中遇到了一些問題,所以下面這篇文章主要給大家介紹了關(guān)于Windows?Server?2016中文版安裝docker的詳細(xì)步驟,需要的朋友可以參考下2022-07-07
Docker使用編寫dockerfile啟動node.js應(yīng)用
這篇文章主要介紹了Docker使用編寫dockerfile啟動node.js應(yīng)用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01

