nginx automatically generates configuration files in docker container

nginx automatically generates configuration files in docker container

When a company builds Docker automated deployment, it needs to create an nginx image so that when the Docker runs, the configuration file in the container is automatically generated by externally specifying environment variables, without having to modify the configuration file in the container.

Implementation ideas

The final command you run is probably like this:

docker run -d -p 80:80 -e xxx=xx Image name Script path in the image

The script here will replace the CMD instruction in the dockerfile, so we need to build a shell script that automatically generates and starts nginx.

#!/bin/bash

#Get the lt beginning from the environment variable 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
#Replace the variable result in the nginx_conf configuration file
sed -i "s|nginx_conf|$(echo ${result})|g" /etc/nginx/nginx.conf
cd /usr/sbin
./nginx

One thing that needs to be explained is that the entire configuration file does not need to be generated in the business. It is only necessary to generate the location and then replace the location marked in the original configuration file. The following is the location marked in the original configuration file.

http {
  ...
  
  server {
    ...

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

    nginx_conf

    #error_page 404 /404.html;
    ...

I thought that putting this shell script and the default configuration file into the nginx dockerfile image would make it successful, but... after running the above command, the container did not start. When I checked the container log, the message that came out was ***Syntax error: “(” unexpected***. My shell script has been tested and can be run on centos, so why is this error reported? After investigation, it turns out that the dockerfile uses the official nginx as the base image. The official image uses Ubuntu and no longer uses bash to execute shell scripts but dash. What a pitfall. I had no choice but to modify the dockerfile. The following is to use 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

# In order to reduce the space occupied by the final generated image, the yum update command is not executed here, which may not be a good practice. # In order to speed up the build, the 163 installation source is used here. #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 the nginx default file COPY nginx.conf /etc/nginx/
#Add a shell script to automatically generate a configuration file COPY script name /root/

#Expose ports EXPOSE 80 443

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

Reminder: The docker container does not support background operation. After the command is executed, the container will exit naturally. Here we need to set the nginx configuration file

#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; //Add here to turn off background running events {
  worker_connections 1024;
}


http {

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Use Docker to install Nginx and configure port forwarding problems and solutions
  • Docker deploys Nginx and configures reverse proxy
  • Docker nginx + https subdomain configuration detailed tutorial
  • Start nginxssl configuration based on docker
  • How to deploy nginx with Docker and modify the configuration file
  • Detailed explanation of nginx plug-in configuration and files under Docker

<<:  JavaScript to achieve time range effect

>>:  Detailed steps for completely uninstalling MySQL 5.7

Recommend

How to use CSS to write different styles according to sub-elements

The effect we need to achieve: What is needed The...

jQuery implements breathing carousel

This article shares the specific code of jQuery t...

Example of how to change the line spacing of HTML table

When using HTML tables, we sometimes need to chan...

Vue + element to dynamically display background data to options

need: Implement dynamic display of option values ...

Two ways to visualize ClickHouse data using Apache Superset

Apache Superset is a powerful BI tool that provid...

How to deploy Spring Boot using Docker

The development of Docker technology provides a m...

Detailed explanation of common usage of pseudo-classes before and after in CSS3

The before/after pseudo-class is equivalent to in...

Detailed process of FastAPI deployment on Docker

Docker Learning https://www.cnblogs.com/poloyy/p/...

Detailed Linux installation tutorial

(Win7 system) VMware virtual machine installation...

Understanding the Lazy Loading Attribute Pattern in JavaScript

Traditionally, developers create properties in Ja...

How to understand Vue front-end and back-end data interaction and display

Table of contents 1. Technical Overview 2. Techni...

Database query optimization: subquery optimization

1. Case Take all employees who are not the head o...

jQuery implements all shopping cart functions

Table of contents 1. Select All 2. Increase or de...