


How nginx automatically generates configuration files in docker containers
Implementation ideas
The last command run is probably like this:
docker run -d -p 80:80 -e xxx=xx 镜像名称 镜像中脚本路径
The script here will replace the cmd command in the dockerfile, so we have to build A shell script that automatically generates and starts nginx.
#!/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
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 I put this shell script and the default configuration file into the nginx dockerfile image, and then it succeeded, but... After running the above command, the container did not start up. Check the container log and see the information. But it is ***syntax error: “(” unexpected***. My shell script has been tested on centos and can be run, so why is this error reported? After investigation, it turns out that the dockerfile uses the basic image as the official nginx , the official image uses ubuntu instead of bash to execute the shell script using dash, which is really a trap. I had no choice but to modify the dockerfile. The following is to use the basic 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 # 为了减小最终生成的镜像占用的空间,这里没有执行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;"]
Reminder: Docker containers do not support the background Run, when the command is executed, the container will naturally exit. 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; //这里添加,关闭后台运行 events { worker_connections 1024; } http {
The above is the detailed content of How nginx automatically generates configuration files in docker containers. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

To get the Docker version, you can perform the following steps: Run the Docker command "docker --version" to view the client and server versions. For Mac or Windows, you can also view version information through the Version tab of the Docker Desktop GUI or the About Docker Desktop menu.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

To save the image in Docker, you can use the docker commit command to create a new image, containing the current state of the specified container, syntax: docker commit [Options] Container ID Image name. To save the image to the repository, you can use the docker push command, syntax: docker push image name [: tag]. To import saved images, you can use the docker pull command, syntax: docker pull image name [: tag].

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

You can switch to the domestic mirror source. The steps are as follows: 1. Edit the configuration file /etc/docker/daemon.json and add the mirror source address; 2. After saving and exiting, restart the Docker service sudo systemctl restart docker to improve the image download speed and stability.
