Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to automatically generate configuration files in a docker container using nginx

Shulou Source: shulou.com Published: 2022-06-01 20:37:18 09月16日 Update

How can I automatically generate a configuration file in a docker container using nginx? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Realization idea

The last command to run looks something like this:

Docker run-d-p 80:80-e script path in the xxx=xx image name mirror

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

#! / bin/bash# gets the beginning of lt from the environment variable to distinguish it from other environment variables For example, lt_analysis=172.17.0.1:8083result= "" for an in $(env | grep ^ lt) do OLD_IFS= "$IFS" IFS= "_" arr= ($a) baked ${arr [1]} IFS= "=" arr= ($b) IFS= "$OLD_IFS" result= "${result} location / ${arr [0]} / {proxy_pass Proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90;} "done# replaces the nginx_conf in the nginx configuration file with the variable resultsed-I" s | nginx_conf | $(echo ${result}) | g "/ etc/nginx/nginx.confcd / usr/sbin./nginx

It is important to note that the entire configuration file does not need to be generated in the business, only the location is generated and then the location of the tag in the original configuration file is replaced. Here is the location of the original configuration file tag.

Http {... Server {... Location / {root html; index index.html index.htm;} nginx_conf # error_page 404 / 404.html;

I thought I would put this shell script and the default configuration file in the dockerfile image of nginx, and then it would be successful, but. After running the above command, the container does not get up. Check the container log, but the message is * Syntax error: "(" unexpected*** ". My shell script has been tested to run on centos, so why report this error? After investigation, it turns out that the basic image used by dockerfile is the official nginx, and the official image uses Ubuntu to execute shell scripts instead of bash, which is really a pit. I have no choice but to modify the dockerfile. The following is to use the basic image centos.

FROM centosENV NGINX_VERSION 1.10.3ENV OPENSSL_VERSION 1.0.2kENV PCRE_VERSION 8.40ENV ZLIB_VERSION 1.2.11ENV BUILD_ROOT / usr/local/xx/nginx# to reduce the space occupied by the resulting image, the yum update command is not executed here, which may not be a good practice # in order to speed up the build The installation 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\ is used here & & 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# replaces nginx default file COPY nginx.conf / etc/nginx/# add shell script COPY script name / root/# exposed port EXPOSE 80 443CMD ["nginx" "- g", "daemon off "]

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

# user nobody;worker_processes 1% error 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, close the background to run events {worker_connections 1024;} http {after reading the above, do you know how to use nginx to automatically generate configuration files in the docker container? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Files scripts generation configuration containers mirrors runs automatic generation commands variables location content original name background foundation official that is methods more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Tech Info Xiaomi Shulou Information MariaDB