【发布时间】:2020-09-25 14:38:33
【问题描述】:
我有一个 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.conf 文件中的任何内容时,我正在寻找一种方法让容器内的 nginx 服务自动重新加载 (nginx -s reload) conf.d 文件夹。
我找到的最接近的就是这个教程:https://cyral.com/blog/how-to-auto-reload-nginx/
但是我不得不稍微调整一下路径,我不知道openresty 是什么,我想它是自定义图像还是什么? (Docker noob here)...无论如何,我已经从该链接尝试了以下内容:
创建了 docker-entrypoint.sh 和 nginxReloader.sh 文件:
docker-entrypoint.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 时,它要么在nginxReloader.sh 文件中的exec "$@" 行中出现No such file or directory 错误,要么在执行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 循环)。
另外,如果我完全删除 Dockerfile 中的 ENTRPOINT 行,在运行 docker-compose up 时,我仍然会得到以下输出:
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
【问题讨论】:
标签: docker nginx docker-compose