gpt4 book ai didi

Docker - Nginx 自动重新加载配置文件更改

转载 作者:行者123 更新时间:2023-12-04 03:54:51 25 4
gpt4 key购买 nike

我有一个 nginx 容器,带有以下 Dockerfile:

FROM nginx:1.19.2

COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./conf.d /etc/nginx/conf.d

WORKDIR /etc/nginx/conf.d

RUN ln -s /etc/nginx/conf.d/my-site/my-domain.generic.conf \
&& ln -s /etc/nginx/conf.d/my-site/my-domain.conf

COPY ./certs/* /etc/ssl/

我有以下 docker-compose 文件:

version: '3.5'

services:
my-site_nginx:
container_name: my-site_nginx
build:
context: ./nginx
network: host
image: my-site_nginx
ports:
- '80:80'
- '443:443' # SSL
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d:ro

当我更改 conf.d 文件夹中的任何内容时,我正在寻找一种方法让容器内的 nginx 服务自动重新加载(nginx -s reload)以及与 conf.d 文件夹位于同一级别的 nginx.conf 文件。

我发现的最接近的是这里的教程:https://cyral.com/blog/how-to-auto-reload-nginx/但是我不得不稍微调整一下路径,我不知道 openresty 是什么,我想它是自定义图像还是什么? (这里是 Docker noob)...无论如何,我已经从该链接尝试了以下内容:

创建了 docker-entrypoint.sh 和 nginxReloader.sh 文件:

docker 入口点.sh:

#!/bin/bash
###########

sh -c "nginxReloader.sh &"
exec "$@"

nginxReloader.sh:

#!/bin/bash
###########

while true
do
inotifywait --exclude .swp -e create -e modify -e delete -e move /etc/nginx/conf.d
nginx -t
if [ $? -eq 0 ]
then
echo "Detected Nginx Configuration Change"
echo "Executing: nginx -s reload"
nginx -s reload
fi
done

并将其添加到 Dockerfile 中:

# [...]

COPY ./nginxReloader.sh /usr/local/bin/nginxReloader.sh
COPY ./docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/nginxReloader.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

RUN apt-get update && apt-get install -y --no-install-recommends apt-utils
RUN apt-get install inotify-tools -y

ENTRYPOINT [ "/usr/local/bin/docker-entrypoint.sh" ]
# CMD ["/usr/local/openresty/bin/openresty", "-g", "daemon off;"] (don't know what this would do, but I wouldn't know what to replace `openresty` with in my case, so I omitted this line from the tutorial at the link I provided)

但是,当尝试 docker-compose up --build 时,它会出现 No such file or directory for line exec "$@" 错误> 在 nginxReloader.sh 文件中或者我在执行 docker-compose up 时得到了 nginx exited with code 0 (当然,我尝试了不同的东西在这些错误之间,但不记得到底是什么)。

此外,我尝试将 Dockerfile 中的 ENTRYPOINT 直接指向 nginxReloader.sh (ENTRYPOINT [ "/usr/local/bin/nginxReloader.sh"]) 但是当尝试 docker -compose up 我只得到 2 行输出:

Setting up watches.
Watches established.

nginx 永远不会启动(我想这是因为 while true 循环)。

此外,如果我在运行 docker-compose up 时完全删除 Dockerfile 中的 ENTRPOINT 行,我仍然会得到以下输出:

my-site_nginx    | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
my-site_nginx | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
my-site_nginx | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
my-site_nginx | 10-listen-on-ipv6-by-default.sh: error: can not modify /etc/nginx/conf.d/default.conf (read-only file system?)
my-site_nginx | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
my-site_nginx | /docker-entrypoint.sh: Configuration complete; ready for start up

就像 Docker 以某种方式知道该文件在文件夹中,与 Dockerfile 处于同一级别...没有错误,但更改配置仍然不会触发 nginx -s reload

最佳答案

你的问题是当你用你的新入口点覆盖它时你没有运行原始入口点,所以 nginx 将不会启动。改变

docker-entrypoint.sh:

#!/bin/bash
###########

sh -c "nginxReloader.sh &"
exec "$@"

docker-entrypoint.sh:

#!/bin/bash
###########

sh -c "nginxReloader.sh &"
exec /docker-entrypoint.sh "$@"

关于Docker - Nginx 自动重新加载配置文件更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64066263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com