nginx automatically generates configuration files in the docker container

  • 2021-01-25 08:10:01
  • OfStack

When a company builds an automated docker deployment, it needs to create an nginx image that automatically generates configuration files in the container through external environment variables specified in its docker run. There is no need to change configuration files in the container.

Implementation approach

The final command to run looks something like this:


docker run -d -p 80:80 -e xxx=xx  Name of the mirror   The script path in the image 

The script here will replace the CMD directive in dockerfile, so we will build an shell script that is automatically generated and starts nginx.


#!/bin/bash

# Gets it from an environment variable lt In order to distinguish it from other environment variables, for example 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
# will nginx In the configuration file nginx_conf Is substituted into a variable result
sed -i "s|nginx_conf|$(echo ${result})|g" /etc/nginx/nginx.conf
cd /usr/sbin
./nginx

One point to note is that the business does not need to generate the entire configuration file, it only needs to generate location and replace the location of the tags in the original configuration file. Here is the location of the tags in the original configuration file.


http {
  ...
  
  server {
    ...

    location / {
      root  html;
      index index.html index.htm;
    }

    nginx_conf

    #error_page 404       /404.html;
    ...

I thought I put the shell script and the default configuration file into the dockerfile image of nginx and it worked, but... ***Syntax error: "(" unexpected***") unexpected: "(" unexpected***") "unexpected:" (" unexpected*** ") ") "unexpected:" (" unexpected*** ")" My shell script has been tested to work on centos, so why report this error? ES34en is not used as ES35en, but instead ES36en is used to execute the ES37en script. I had no choice but to modify dockerfile, and now I'm using the base image 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

#  To minimize the space taken up by the resulting image, this is not done yum update Commands may not be good practice 
#  To speed up the build, this is used 163 The installation of the source 
#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

# replace nginx The default file 
COPY nginx.conf /etc/nginx/
# Added automatic configuration file generation shell The script 
COPY  The name of the script  /root/

# Exposure to port 
EXPOSE 80 443

CMD ["nginx", "-g", "daemon off;"]

Note: The nginx container does not support running in the background. When the command is executed, the container will exit naturally. Here we need to set the nginx configuration file 1


#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;  // So I'm going to add, close the background 
events {
  worker_connections 1024;
}


http {


Related articles: