Home Operation and Maintenance Nginx How nginx automatically generates configuration files in docker containers

How nginx automatically generates configuration files in docker containers

May 18, 2023 pm 10:37 PM
docker nginx

Implementation ideas

The last command run is probably like this:

docker run -d -p 80:80 -e xxx=xx 镜像名称 镜像中脚本路径
Copy after login

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
Copy after login

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;
    ...
Copy after login

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;"]
Copy after login

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 {
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

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.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

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).

How to restart docker How to restart docker Apr 15, 2025 pm 12:06 PM

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

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 start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to view the docker process How to view the docker process Apr 15, 2025 am 11:48 AM

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

See all articles